エラーの発生および処理
このフレームワークでは、復旧できないアプリケーションエラーおよび復旧できるアプリケーションエラーを JavaScript コードで柔軟に対処できます。たとえば、action.setCallback() を使用して、サーバ側の応答の処理時にこれらのエラーを発生させることができます。
復旧できないエラー
アプリケーションが正常に起動できないエラーなどの復旧できないエラーには、$A.error("error message here") を使用します。これには、ページのスタック追跡が表示されます。
復旧できるエラー
復旧できるエラーを処理するには、ui:message などのコンポーネントを使用して、その問題についてユーザに通知します。
次のサンプルでは、JavaScript コントローラでの基本的なエラーの発生およびキャッチを示します。
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>
24クライアント側コントローラのソースを次に示します。
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})
52コントローラコードがエラーを発生させてキャッチします。エラーのメッセージは、動的に作成される ui:message コンポーネントでユーザに表示されます。ui:message のボディは、エラーテキストを含む ui:outputText コンポーネントです。