0

My aura component still retrieve a response with 'SUCCESS' state even though I threw Exception. My aura component's controller

handleApproval: {
 var recordId = cmp.get("v.recordId");
 var action = cmp.get("c.handleApproval");
 action.setParams({
  recordId: recordId
 });
 action.setCallback(this, function(response) {
  console.log("result: ", JSON.stringify(response));
  var state = response.getState();
  console.log("state: ", state); //retrun SUCCESS here
 }
}

my Apex Controller:

[...]
@AuraEnabled
public static String handleApproval(String recordId) {
 String error = null
 try {
  [...]
  if(condition) { // condition returns true
   error = 'Failed to submit for approval';
   throw new AuraHandledException(error);
  }
  [...]
 } catch (Exception e) {
  error = 'Failed to submit for approval. Error: ' + e.getMessage();
 }
}
[...]

How can I return 'ERROR' state in this case? Please help.

2
  • 1
    According to the partial code posted, you threw exception in the if block, but it has been catched from the same apex apex method and never rethrew, so the method didn't fail, that's why you see SUCCESS in the aura component.
    – RubenDG
    Jun 18, 2023 at 11:58
  • Thanks @RubenDG, I throw an exception in catch blocks and it works. Jun 18, 2023 at 13:02

1 Answer 1

2

The issue is that the exception is catched by the same method, so it never fails, therefore the state returned in the aura component is SUCCESS.

In order to get the ERROR state you should rethrow the exception in the catch block:

try {
    [...]
    if(condition) { // condition returns true
        error = 'Failed to submit for approval';
        throw new AuraHandledException(error);
    }
    [...]
} catch (Exception e) {
    error = 'Failed to submit for approval. Error: ' + e.getMessage();
    throw e; // rethrow the Exception
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .