---
schema_version: "1"
id: 106988f9629e51ae10f76ddd15763eba4cf15daf8c3ec904be5c008bf3e31383
title: Handling Errors
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/platform/aura-platform/guides/aura/data-service/data-service-handling-errors.md
canonical_url: https://developer.salesforce.com/docs/platform/aura-platform/guide/data-service-handling-errors.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: 3b97465115d58e2562013d6c27a6342ce044feec5e6be0cb5dd5a85cef7a8837
products:
  - Lightning Aura Components Developer Guide
  - Platform Developer Content
---

# Handling Errors

Lightning Data Service returns an error when a resource, such as a record or an object, is inaccessible on the server.

For example, an error occurs if you pass in an invalid input to the form-based components, such as an invalid record ID or missing required fields. An error is also returned if the record isn’t in the cache and the server is offline. Also, a resource can become inaccessible on the server when it’s deleted or has its sharing or visibility settings updated.

## Handle Errors For Form-Based Components

Two types of errors—field-level errors and Lightning Data Service errors—are handled by `lightning:recordForm`, `lightning:recordEditForm`, and `lightning:recordViewForm`. Field-validation errors display below a field and cannot be customized. For example, an error is shown below a required field when it’s empty. Lightning Data Service errors are handled in the following ways.

**`lightning:recordForm`**

Automatically displays an error message above the form fields. You can provide additional error handling using the `onerror` event handler.

**`lightning:recordEditForm`**

To automatically display an error message above or below the form fields, include `lightning:messages` before or after your `lightning:inputField` components.

You can provide additional error handling using the `onerror` event handler.

**`lightning:recordViewForm`**

To automatically display an error message above or below the form fields, include `lightning:messages` before or after your `lightning:outputField` components.

If a single field has multiple validation errors, the form shows only the first error on the field. Similarly, if a submitted form has multiple errors, the form displays only the first error encountered. When you correct the displayed error, the next error is displayed.

The error object looks like this.

```javascript
{
  "message": "Disconnected or Canceled",
  "detail": "",
  "output": {
    
  },
  "error": {
    "ok": false,
    "status": 400,
    "statusText": "Bad Request",
    "body": {
      "message": "Disconnected or Canceled"
    }
  }
}
```

Get the error object using this syntax.

```javascript
handleError: function (cmp, event, helper) {
    var error = event.getParams();

    // Get the error message
    var errorMessage = event.getParam("message");
}
```

## Handle Errors For `force:recordData`

To act when an error occurs, handle the `recordUpdated` event and handle the case where the `changeType` is “ERROR”.


## Example

Declare that your component handles the `recordUpdated` event.

```nolang
<force:recordData aura:id="forceRecord"
    recordId="{!v.recordId}"
    fields="Name,Title,Email"
    targetRecord="{!v._record}"
    targetFields="{!v.simpleRecord}"
    targetError="{!v._error}"
    recordUpdated="{!c.recordUpdated}" />
```

Implement an action handler that handles the error.

```javascript
({
  recordUpdated: function(component, event, helper) {

    var changeType = event.getParams().changeType;

    if (changeType === "ERROR") { /* handle error; do this first! */ }
    else if (changeType === "LOADED") { /* handle record load */ }
    else if (changeType === "REMOVED") { /* handle record removal */ }
    else if (changeType === "CHANGED") { /* handle record change */ }
})
```

If an error occurs when the record begins to load, `targetError` is set to a localized error message. An error occurs if:

-   Input is invalid because of an invalid attribute value, or combination of attribute values. For example, an invalid `recordId`, or omitting both the `layoutType` and the `fields` attributes.
-   The record isn’t in the cache and the server is unreachable (offline).

If the record becomes inaccessible on the server, the `recordUpdated` event is fired with `changeType` set to "REMOVED." No error is set on `targetError`, since records becoming inaccessible is sometimes the expected outcome of an operation. For example, after lead convert the lead record becomes inaccessible.

Records can become inaccessible for the following reasons.

-   Record or entity sharing or visibility settings restrict access.
-   Record or entity is deleted.

When the record becomes inaccessible on the server, the record’s JavaScript object assigned to `targetRecord` is unchanged.

