The below code sample given is for Salesforce but it is close to Java and you should be able to understand easily what I am trying to ask here.
AccountController.getAccountsThatMeetSpecificCrtieria
is a method invoked by front end(ex: jsp) and if this method runs into any error, Salesforce says, we have to throw specific exception i.e. AuraHandledException
So, in the below example, I am saying AccountUtil.getAccounts
should throw front end agnostic exception but my team thinks that it should throw front end friendly exception as it is indirectly called from front end.
What is the recommended approach here and what is the principle behind it?
AccountController class:
public class AccountController {
@AuraEnabled
public static void getAccountsThatMeetSpecificCrtieria() {
try {
AccountUtil.getAccounts(UserInfo.getUserId());
} catch(Exception e) {
throw new AuraHandledException('An error occurred');
}
}
}
AccountUtil class:
public class AccountUtil {
public static List<Account> getAccounts(Id userId) {
try {
// do some logic
} catch(Exception e) {
throw new AccountUtilException(e);
}
}
public class AccountUtilException extends Exception {}
}
-
I believe it allows for custom user friendly messages as opposed to a generic "Internal Server Error" messaging. If you throw an Aura Exception users will see a friendly message of your design ==> throw new AuraHandledException('Your friendly exception goes here'). In your case, 'An error occurred' isn't that friendly or useful, so if that is the case, just throw the original exception. It would depend on whether you can present some useful information to your end users for the error. You don't want to expose internal error messaging as users will not know what to make of it.Jon Raynor– Jon Raynor2023年09月19日 19:20:01 +00:00Commented Sep 19, 2023 at 19:20
1 Answer 1
I think Salesforce is an.. ( •_•)>⌐■しかく-■しかく exception to the general rule here as it requires this specific type of exception to be thrown in controllers that are used by "Aura Components" for error handling.
The general rule is to let the exception bubble up and not catch and rethrow it. Handle all exceptions at the highest level you can, in this case the front end, which shouldn't be linked to a specific exception type.