1

I have an aura controller which calls a Services class. This services class makes a callout to external system and returns a response.

public class Anauracontroller{
  public resObj callService(){
    try{
      ServicesToCall.makeCall();
    }Catch(AuraException ex){

    }
  } 
}

Service class:

public class ServicesToCall{

 public static resObj makeCall(){

 }

}

Please pardon any coding standards that I might have violated here.

My question: Can an auraException handle all sort of exception- Example if there is any CalloutException? Does it make sense to create a customException here?

3

You can catch any kind of catchable exception (not, for example, LimitException) and re-raise it as an AuraHandledException to handle it cleanly in your client-side JavaScript controller.

try {
    ServicesToCall.makeCall();
} catch (CalloutException ex) {
    throw new AuraHandledException(ex.getMessage());
}

Creating the exception by passing a string to its constructor ensures that you get a 'nice' display in Lightning with a user-friendly message (well, as user-friendly as the underlying exception's message, here), and not a long and technical stack trace.

3
  • Thanks. I was wondering if it is a exception due to callout - like if the service was not available. How would that be displayed in auraHandler exception's getMessage() method? – SfdcBat Nov 16 '18 at 15:47
  • 1
    The error message that your JavaScript controller will access on the response object will be whatever you pass to AuraHandledException's constructor. You can decide whether you want that to be the underlying exception's message or a custom string. – David Reed Nov 16 '18 at 15:50
  • 1
    @SfdcBat To add pointer on what DavidReed was saying read Basic Error Handling – codeyinthecloud Nov 16 '18 at 15:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.