Newer Version Available
Throwing and Handling Errors
Unrecoverable Errors
Use $A.error("error message here") for unrecoverable errors, such as an error that prevents your app from starting successfully. It shows a stack trace on the page.
Recoverable Errors
To handle recoverable errors, use a component, such as ui:message to tell the user about the problem.
This sample shows you the basics of throwing and catching an error in a JavaScript controller.
1swfobject.registerObject("clippy.codeblock-0", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17<!--docsample:recoverableError-->
18<aura:component>
19 <p>Click the button to trigger the controller to throw an error.</p>
20 <div aura:id="div1"></div>
21
22 <ui:button label="Throw an Error" press="{!c.throwErrorForKicks}"/>
23</aura:component>
24Here is the client-side controller source.
1swfobject.registerObject("clippy.codeblock-1", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17/*recoverableErrorController.js*/
18({
19 throwErrorForKicks: function(cmp) {
20 // this sample always throws an error to demo try/catch
21 var hasPerm = false;
22 try {
23 if (!hasPerm) {
24 throw new Error("You don't have permission to edit this record.");
25 }
26 }
27 catch (e) {
28 $A.createComponents([
29 ["ui:message",{
30 "title" : "Sample Thrown Error",
31 "severity" : "error",
32 }],
33 ["ui:outputText",{
34 "value" : e.message
35 }]
36 ],
37 function(components, status){
38 if (status === "SUCCESS") {
39 var message = components[0];
40 var outputText = components[1];
41 // set the body of the ui:message to be the ui:outputText
42 message.set("v.body", outputText);
43 var div1 = cmp.find("div1");
44 // Replace div body with the dynamic component
45 div1.set("v.body", message);
46 }
47 }
48 );
49 }
50 }
51})
52The controller code throws an error and catches it. The message in the error is displayed to the user in a dynamically created ui:message component. The body of the ui:message is a ui:outputText component containing the error text.