User Interface20% of the exam

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.

Official exam objective
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.body correctly for Apex and for Lightning Data Service wire adapters, and catch errors from asynchronous calls.
  • Pick the display: lightning-messages in a record edit form, field-level validity on lightning-input, a toast for page-level outcomes.
  • Make Visualforce messages appear after a partial page update, and know where a trigger's addError message 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

Unhandled system exception returned to Aura
Detail
Details stripped; shows "An internal server error has occurred"
Aura error read
Detail
State ERROR; message in response.getError()
AuraHandledException support
Detail
Aura and LWC; no body.stackTrace
getRecord error body
Detail
Array of objects
Apex error body
Detail
Object
try/catch
Detail
Catches synchronous code only; await the promise
lightning-messages
Detail
Before or after the input fields; no handler needed
Custom field error
Detail
setCustomValidity() then reportValidity(); "" clears
Preferred toast
Detail
lightning/toast
Partial update scope
Detail
Only the identified components and their children
Field-level trigger error
Detail
field.addError() or addError(fieldName, msg)
Sources for this section (8)
  • 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

Practice for the Platform Developer II exam