Handle Server Errors
Learning Objectives
After completing this unit, you’ll be able to:
- Explain how to handle server errors that occur when you wire a property.
- Explain how to handle server errors that occur when you wire a function.
- Explain how to handle server errors that occur when you call an LDS function or Apex imperatively.
- Identify the recommended way to interact with data and handle errors for a specific use case.
Handle Server Errors in Lightning Web Components
Errors thrown by LDS wire adapters, LDS functions, and calls to Apex have specific structures. To retrieve information about an error, you process the error response in your JavaScript code. Then you can show the content of the error to the user.
As the developer, you decide how to present errors to the user: an error panel, a toast message, or something else. For learning purposes, the examples in this module surface errors by referencing an errors
property in the markup, like this:
errors.html
<template> <template if:true={errors}> <p>{errors}</p> </template> </template>
How you handle server errors in JavaScript depends on how you’re interacting with Salesforce data. Let’s explore three examples: wired properties, wired functions, and imperative function calls.
Handling Errors on Wired Properties
When you use @wire
to decorate a property, errors are accessible on the property error
attribute. This is valid when you use @wire
with an LDS wire adapter or with Apex.
wireApexProperty.js
import { LightningElement, api, wire } from 'lwc'; import { reduceErrors } from 'c/ldsUtils'; import getContactsBornAfter from '@salesforce/apex/ContactController.getContactsBornAfter'; export default class WireApexProperty extends LightningElement { @api minBirthDate; @wire(getContactsBornAfter, { birthDate: '$minBirthDate' }) contacts; get errors() { return (this.contacts.error) ? reduceErrors(this.contacts.error) : []; } }
Code highlights:
- Line 2: We import the
reduceErrors
helper function from theldsUtils
module. (You add theldsUtils
module to your project later in this unit.) - Lines 6–7: We decorate the
contacts
property with@wire
to wire it to thegetContactsBornAfter
function. - Line 8: We define a getter function that creates a property named
errors
. Every timethis.contacts.error
changes, the getter updates the value of theerrors
property. This occurs because of reactivity. - Line 10: In the getter, we use the
reduceErrors
helper function to formatthis.contacts.error
. The function reduces the received error objects and returns an array of all the error messages that have occurred.
Handling Errors on Wired Functions
When you use @wire
to decorate a function, the function receives as a parameter an object that includes errors (if there are any errors). This applies when you use @wire
with either LDS wire adapters or Apex.
wireApexFunction.js
import { LightningElement, api, wire } from 'lwc'; import { reduceErrors } from 'c/ldsUtils'; import getContactsBornAfter from '@salesforce/apex/ContactController.getContactsBornAfter'; export default class WireApexFunction extends LightningElement { @api minBirthDate; errors; @wire(getContactsBornAfter, { birthDate: '$minBirthDate' }) wiredContacts({data, error}) { if (error) this.errors = reduceErrors(error); } }
Code highlights:
- Line 2: We import the
reduceErrors
helper function from theldsUtils
module (as we did in the wireApexProperty example). - Line 3: We import the
getContactsBornAfter
function from theContactController
class. - Line 6: We define the
errors
property. - Lines 7–8: We decorate the
wiredContacts
function with@wire
to wire it to thegetContactsBornAfter
function. - Line 10: Each time the wired function receives an error, we use the
reduceErrors
helper function to format it. The function returns an array of all the errors that have occurred.
Handling Errors When Calling a Function Imperatively
If you call an LDS function or Apex method imperatively, the server returns errors as a parameter to the catch method’s callback function.
callApexImperative.js
import { LightningElement, api, wire } from 'lwc'; import { reduceErrors } from 'c/ldsUtils'; import getContactsBornAfter from '@salesforce/apex/ContactController.getContactsBornAfter'; export default class CallApexImperative extends LightningElement { @api minBirthDate; errors; handleButtonClick() { getContactsBornAfter({ birthDate: this.minBirthDate }) .then(contacts => { // code to execute if the promise is resolved }) .catch(error => { this.errors = reduceErrors(error); // code to execute if the promise is rejected }); } }
Code highlights:
- Line 2: We import the
reduceErrors
helper function. - Line 6: We define a property named
errors
. - Line 8: We invoke the
getContactsBornAfter
function imperatively. The function returns a promise. - Line 11–13: If the promise is resolved, we process
contacts
. - Line 14–16: If the promise is rejected, we use the
reduceErrors
helper function to format the received error and store it in theerrors
property.
Handle Errors in the accountList Component
Let’s add error handling to the accountList component that you created.
- In the accountList.html file, after the
<template>
that includes thelightning-datatable
, add this code:<template if:true={errors}> <p>{errors}</p> </template>
- Copy the
ldsUtils
folder from LWC recipes and include it in the force-app/main/default/lwc directory in your project. This component contains thereduceErrors
function. - Import the
reduceErrors
function near the beginning of accountList.js.import { reduceErrors } from 'c/ldsUtils';
- In accountList.js, insert this getter, which defines an
errors
property:get errors() { return (this.accounts.error) ? reduceErrors(this.accounts.error) : []; }
- To test the error handling, force the
getAccounts
method (in AccountController.cls) to throw an error by commenting the body of the method (temporarily), and adding this code in its place:throw new AuraHandledException('Forced error');
- Save the three files that you edited: accountList.html, accountList.js, and AccountController.cls.
- Deploy the project files (from
force-app/main/default
). - If it’s not already open, open your Trailhead Playground.
- To check the result, refresh the Working with Data page.
Hint: Because Lightning Data Service caches results, you may need to clear the cache before you can see your forced error in action.
Summary
Now you know several ways to interact with Salesforce data in your Lightning web components. Some solutions are preferred to others under certain circumstances. This table summarizes recommended solutions by use case.
Use Cases for Interacting with Salesforce Data
Use Case | Recommended Solution | Notes |
---|---|---|
View or edit a record Field positions are determined by the component. | lightning-record-form | |
View a record You choose which fields to include and where to position them. You can also use standard rendering or provide your own rendering for each field. | lightning-record-view-form | |
Edit a record You choose which fields to include and where to position them. You can also use standard rendering or provide your own rendering and value for each field. | lightning-record-edit-form | |
Read data for one or more records | LDS wire adapters: getRecord or getRecords |
|
Create, edit, or delete one record | LDS functions: createRecord, updateRecord or deleteRecord | Can be combined, but operations run in independent transactions |
Create, edit, or delete multiple records | Call Apex imperatively | |
Read metadata for one or more records | LDS wire adapters: getObjectInfo or getObjectInfos |
|
Read a related list’s metadata or records | LDS wire adapters: getRelatedListInfo and getRelatedListRecords (or the batch versions) | |
Read a listview’s metadata | LDS wire adapters: getListInfoByName (or the batch version) | |
Use cases not covered by any of the above | Call Apex with |
When you work with data in Lightning web components, error handling varies. How you access errors depends on how you’re interacting with the data.
Handling Server Errors
How to Work with Data | How to Access Server Errors |
---|---|
Use either an LDS wire adapter or an Apex method, and decorate a property | Use |
Use either an LDS wire adapter or an Apex method, and decorate a function | Use decoratedFunction({data, error}) {...} |
Imperatively invoke either an LDS wire function or an Apex method | Use functionToInvoke() .then(data => {...}) .catch(error => {...}); |
Wrap Up
In this module, you learned about different ways to work with Salesforce data in Lightning web components. You learned the pros and cons of each technique, when each is recommended, and how to implement each solution. You also learned how to handle server errors in Lightning web components, based on how you interact with Salesforce data.
To keep learning about working with Salesforce data in Lightning web components, check out the resources provided in this module. Also, see Quick Start: Explore the Recipes Sample App for more examples and the code to implement them.
Resources
- Lightning Web Components Developer Guide: Handle Errors in Lightning Data Service
- Blog: Error Handling Best Practices for Lightning Web Components