PDII practice question 4 of 10

A user receives the generic "An internal server error has occurred” while interacting with a custom Lightning component. What should the developer do to ensure a more meaningful message?

Full set: 198 questions
Answer options
A: Add an onerror event handler to the tag.
B: Add an error-view component to the markup.
C: Use platform events to process the error
D: Use an AuraHandledexception in a try-catch block. Correct answer
Correct Answer:
D. Use an AuraHandledexception in a try-catch block.
Explanation

When an unhandled exception is thrown in an Apex controller method called by a Lightning component (LWC or Aura), the Salesforce framework obscures the original error details and returns a generic "An internal server error has occurred" message to the client for structural security. To provide a meaningful, user-friendly error message, the developer must encapsulate the Apex logic within a try-catch block, catch the original exception, and explicitly throw an AuraHandledException. The AuraHandledException is specifically designed to safely bypass the framework's default obscuration in Lightning component contexts. By instantiating an AuraHandledException with a custom message string, the developer ensures that the client-side JavaScript can access and gracefully display the precise error details to the end-user.

Why Incorrect

Option A: Adding an onerror event handler to an HTML tag only handles standard DOM/client-side errors, but it does not unmask server-side generic internal server errors sent by Apex.
Option B: Adding an error-view component simply changes the UI layout or presentation; it does not resolve the framework's suppression of the server-side message detailing the actual Apex failure.
Option C: While platform events are powerful for asynchronous publish/subscribe messaging, they are unnecessarily complex for this synchronous client-server transaction and do not directly return a readable server message to the component.

References

Salesforce Lightning Web Components Developer Guide: "Handle Server Errors", Section: Call Apex Methods.

Salesforce Aura Components Developer Guide: "Returning Errors from an Apex Server-Side Controller", Section: Apex Controllers.

Scroll to Top