In Salesforce documentation, it is mentioned that Aura-enabled Apex methods should throw an AuraHandledException to propagate user-friendly error messages to the client. However, I have a custom exception class in my codebase, and I’m curious about the differences in behavior when throwing a custom exception versus an AuraHandledException.
For example:
public class CustomException extends Exception {
}
If I use this custom exception in my code:
throw new CustomException('Something went wrong.');
And compare it with:
throw new AuraHandledException('Something went wrong.');
My Observations :
On the client side, the error body appeared similar in both cases.
However, I noticed a difference in the Apex Debug Logs:
For AuraHandledException, the Status column in the debug logs displayed "Script-thrown exception."
For CustomException, the Status column displayed the custom error message that was provided when the exception was thrown (e.g., "Something went wrong").
My Question:
What are the key differences in behaviour when using these two types of exceptions in an Aura-enabled Apex method?
I would appreciate examples or explanations regarding the use cases for each approach.