Sub topic 13
Returning errors to Lightning components
This chapter assumes you know basic Apex syntax, SOQL and DML, and how try and catch work. It also assumes you have seen custom exceptions and DmlException.
Why a Lightning component needs special handling
A Lightning component calls Apex through methods marked @AuraEnabled. If one of these methods throws an ordinary exception, the component receives only a generic message such as "Script-thrown exception". Users and developers cannot tell what went wrong.
AuraHandledException solves this. It is a built-in exception that Lightning treats as safe to show to the user. The message you give it is sent to the component.
Throwing AuraHandledException
Catch the exception you expect, then throw an AuraHandledException with a readable message.
public with sharing class AccountController {
@AuraEnabled
public static void saveAccount(Account acc) {
try {
insert acc;
} catch (DmlException e) {
AuraHandledException ex = new AuraHandledException(e.getMessage());
ex.setMessage(e.getMessage());
throw ex;
}
}
}The call to setMessage is not optional in practice. In Apex tests, getMessage() on an AuraHandledException created with only a constructor argument returns "Script-thrown exception". Calling setMessage with the same text makes getMessage() return your message in both tests and runtime code.
Choosing the message
e.getMessage() on a DmlException can include internal detail, such as the record Id and field API names. For a better user experience, build the text yourself. The DML methods from the earlier chapter help:
getDmlMessage(i)returns the message for the failed record at indexi.getDmlFieldNames(i)lists the fields involved.
Keep the user-facing text short and free of stack traces or internal class names. Log the full detail separately if you need it for diagnosis.
Reading the error in the component
On the JavaScript side, a rejected Apex call gives you an error object. The message you set is at error.body.message.
import { LightningElement } from 'lwc';
import saveAccount from '@salesforce/apex/AccountController.saveAccount';
export default class AccountForm extends LightningElement {
errorMessage;
async handleSave() {
try {
await saveAccount({ acc: { Name: this.name } });
this.errorMessage = undefined;
} catch (error) {
this.errorMessage = error.body.message;
}
}
}Common mistakes
| Mistake | Result |
|---|---|
| Letting DmlException propagate from an `@AuraEnabled` method | The component gets a long, unfriendly system message |
| Skipping `setMessage` | Tests see "Script-thrown exception" instead of your text |
| Catching `Exception` and hiding the cause | Users see an error, but you lose the detail needed to fix it |
| Putting sensitive data in the message | Any user running the component can read it |
Exercise
A test asserts that the message of a thrown AuraHandledException contains "Name is required", but the assertion fails and getMessage() returns "Script-thrown exception". What is the likely cause and the fix?
Show answer
The exception was created with only a constructor argument, and getMessage() does not return it in this case. Call setMessage with the same text before throwing, for example ex.setMessage('Name is required'), and the test will see that text.
Exercise
Where in the JavaScript error object do you find the message that an AuraHandledException carried?
Show answer
In error.body.message.