Handling Errors
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.
1{
2 "message": "Disconnected or Canceled",
3 "detail": "",
4 "output": {
5
6 },
7 "error": {
8 "ok": false,
9 "status": 400,
10 "statusText": "Bad Request",
11 "body": {
12 "message": "Disconnected or Canceled"
13 }
14 }
15}Get the error object using this syntax.
1handleError: function (cmp, event, helper) {
2 var error = event.getParams();
3
4 // Get the error message
5 var errorMessage = event.getParam("message");
6}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
1<force:recordData aura:id="forceRecord"
2 recordId="{!v.recordId}"
3 fields="Name,Title,Email"
4 targetRecord="{!v._record}"
5 targetFields="{!v.simpleRecord}"
6 targetError="{!v._error}"
7 recordUpdated="{!c.recordUpdated}" />1({
2 recordUpdated: function(component, event, helper) {
3
4 var changeType = event.getParams().changeType;
5
6 if (changeType === "ERROR") { /* handle error; do this first! */ }
7 else if (changeType === "LOADED") { /* handle record load */ }
8 else if (changeType === "REMOVED") { /* handle record removal */ }
9 else if (changeType === "CHANGED") { /* handle record change */ }
10})- 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.
- 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.