Displaying errors in LWC, Aura and Visualforce
How a server or input error reaches the user: AuraHandledException instead of a stripped system exception, the different error.body shapes from Apex and Lightning Data Service, awaiting imperative calls, lightning-messages, lightning-input validity, toasts, apex:pageMessages with reRender, and how trigger addError messages surface.
Given a scenario, identify best practices for displaying errors in the user interface.
~7 min read · 7 practice questions ·
What the exam expects
- Return a readable server error to Aura and LWC with
AuraHandledException, and read it on the client. - Read
error.bodycorrectly for Apex and for Lightning Data Service wire adapters, and catch errors from asynchronous calls. - Pick the display:
lightning-messagesin a record edit form, field-level validity onlightning-input, a toast for page-level outcomes. - Make Visualforce messages appear after a partial page update, and know where a trigger's
addErrormessage shows up.
Key terms: AuraHandledException (an exception whose message Apex chooses and the client receives), error.body (the response body on an LWC error, whose shape depends on the API), lightning-messages (the component that shows a record edit form's errors), custom validity (setCustomValidity() plus reportValidity() on an input), <apex:pageMessages> (Visualforce's display of all messages for the page).
How addError behaves inside triggers is owned by Advanced trigger context and handler patterns; this unit covers how messages reach the screen. The PD I basics are in the Platform Developer guide's Apex for page components: controllers and wire adapters.
Key facts
- Detail
- Details stripped; shows "An internal server error has occurred"
- Detail
- State ERROR; message in
response.getError()
AuraHandledException support- Detail
- Aura and LWC; no
body.stackTrace
getRecord error body- Detail
- Array of objects
- Detail
- Object
try/catch- Detail
- Catches synchronous code only; await the promise
lightning-messages- Detail
- Before or after the input fields; no handler needed
- Detail
setCustomValidity()thenreportValidity();""clears
- Detail
lightning/toast
- Detail
- Only the identified components and their children
- Detail
field.addError()oraddError(fieldName, msg)
Sources for this section (8)
- Returning Errors from Apexdeveloper.salesforce.com (opens in a new tab)
- Handle Errors from Apexdeveloper.salesforce.com (opens in a new tab)
- Handle Errors in LDSdeveloper.salesforce.com (opens in a new tab)
- lightning-record-edit-formdeveloper.salesforce.com (opens in a new tab)
- lightning-inputdeveloper.salesforce.com (opens in a new tab)
- Toast Notificationsdeveloper.salesforce.com (opens in a new tab)
- Partial Page Updatesdeveloper.salesforce.com (opens in a new tab)
- SObject Classdeveloper.salesforce.com (opens in a new tab)
- Why does an unhandled `DmlException` give an Aura component's users a generic message?
- When is `error.body` an array rather than an object?
- Why can a `try/catch` miss an imperative Apex error?
- Where must `lightning-messages` go to show a record edit form's errors?
The questions are free to read. Answers with their sources and self-grading are included with the pack.
Keep reading with the Platform Developer II pack
- Core concepts
- Exam traps
- Worked scenario
The pack includes:
- All 28 lessons of the Platform Developer II guide, with reading progress
- Interactive decision walks and step-by-step sequence steppers
- Inline practice questions and one-click topic practice (7 questions for this lesson)
- An official source cited for every claim
- An AI tutor that answers from the guide
- Unlimited timed practice exams