Lightning Data Service Returns New Error Values

Lightning Data Service returns an error when a resource, such as a record or an object, is inaccessible on the server. The new error object contains the body of the response and a status code and message.

Where: This change applies to orgs with Lightning web components in Lightning Experience, Lightning Communities, and all versions of the mobile app in all editions. To create a Lightning web component, use Enterprise, Performance, Unlimited, and Developer editions.

Why: The new error object, which is modeled after the Fetch API’s Response object, has a simpler shape to support the UI API and other Salesforce APIs .

How: Previously, you captured the error message using error.message and the error object looked like this.

//Before Spring ’19
    type: enum<string>;
    message: string;
    details: object;

You now capture the error message using error.body.message or error.body[].message, depending on the API that returns it. The error object has been changed as follows.

//After Spring ’19
    body: object|array;
    ok: boolean;
    status: number;
    statusText: string
  • body (object|array)—The body of the response defined by the underlying API.
  • ok (boolean)—Specifies whether the response was successful or not. ok is always set to false.
  • status (number)—Contains the status code of the response, for example, 404 if a resource is not found or 500 for an internal server error.
  • statusText (string)—Contains the status message corresponding to the status code, for example, Not Found for a status code of 404.

The body of the error response depends on the API that returns it.

  • UI API read operations, such as the getRecord wire adapter, return error.body as an array of objects.
  • UI API write operations, such as the createRecord wire adapter, return error.body as an object, often with object-level and field-level errors.
  • Apex read and write operations return error.body as an object.
  • Network errors, such as an offline error, return error.body as an object.