5

My Apex controller method might throw a custom exception

@AuraEnabled
public static Id myAction() {
    // Provoke exception
    Integer foo = 10 / 0;

and I want the toast to display it properly

var toastEvent = $A.get("e.force:showToast");
toastEvent.setParams({
     title: 'Error',
     type: 'error',
     message: response.getError()[0]
});
toastEvent.fire();

Instead of my the string I passed into my custom exception I get this! What am I doing wrong?

enter image description here

3
  • 2
    You want an AuraHandledException instead of your own apex class
    – martin
    Jun 2, 2017 at 12:20
  • Do I have to wrap/ cast/extend my own exceptions with AuraHandledException? And what about System.exceptions? Will they be handled gracefully? Jun 2, 2017 at 12:25
  • Actually, AuraHandledException is not an abstract or virtual class, so it can't be extended.
    – martin
    Jun 2, 2017 at 12:34

3 Answers 3

6

Adding ugly wrapping code to all my @AuraEnabled methods seemed not to be an acceptable clean solution to me...

@AuraEnabled
public static Id myAction() {
   try {
      // regular action code
   }
   catch (Exception ex){
      throw new AuraHandledException(ex.getMessage());
   }
 }

...so I solved it in my JS toast code.

var uglyIndicator = "Caused by: common.apex.runtime.impl.ExecutionException:";
if(message.includes(uglyIndicator)) {
    message = message.split(uglyIndicator)[1];
}
4

I suppose, in case, when system exception could be thrown you need to throw it one more time, but as a AuraHandledException. Something like:

try {
    Decimal result = 322 / 0;
} catch (Exception ex){
    throw new AuraHandledException(ex.getMessage());
}
2
  • By doing that I would also rewrite DmlException and other System exceptions that already behave nicely. Would I have any negative side-effects from doing that? Jun 2, 2017 at 12:36
  • 1
    @RobertSösemann, yes, you should :( Jun 6, 2017 at 7:31
2

There's a particular apex class called an AuraHandledException that you need to use to Return Errors from an Apex Server-Side Controller.

Note that you need to set the message through the class constructor rather than using the Exception.setMessage() method.

// This goes to the ui
AuraHandledException e = new AuraHandledException('Message I want to display');

// This message stays server-side
e.setMessage('Message I want to display');
4
  • What if the Exception is not thrown by my own Apex code? Just imagine I add a division by zero to my above code. The Divide by Zero message is also display as ugly as my custom exception. Jun 2, 2017 at 12:30
  • Yes, the standard system exceptions and custom exceptions all look that way. The AuraHandledException is the only one that looks nice.
    – martin
    Jun 2, 2017 at 12:32
  • Not true. I tried it with DivisionByZero and it's still ugly! Jun 2, 2017 at 12:36
  • Right, the standard system exceptions and custom exceptions all look bad.
    – martin
    Jun 2, 2017 at 12:38

You must log in to answer this question.

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