この文章は Salesforce 機械翻訳システムを使用して翻訳されました。詳細はこちらをご参照ください。
英語に切り替える

エラーの発生

このフレームワークでは、復旧できないアプリケーションエラーおよび復旧できるアプリケーションエラーを JavaScript コードで柔軟に対処できます。

復旧できないエラー

アプリケーションが正常に起動できないエラーなどの復旧できないエラーには、$A.error("error message here") を使用します。これには、ページのスタック追跡が表示されます。

復旧できるエラー

復旧できるエラーを処理するには、ui:messageui:dialog などのコンポーネントを使用して、その問題についてユーザに通知します。

次のサンプルでは、JavaScript コントローラでの基本的なエラーの発生およびキャッチを示します。

コンポーネントのソース

1swfobject.registerObject("clippy.codeblock-0", "9");<aura:component>
2    
3    <br/>
4    
5    <p>Click the button to trigger the controller to throw an error.</p>
6    
7    <br/>
8    <div aura:id="div1"></div>
9
10    <ui:button label="Throw an Error" press="{!c.throwErrorForKicks}"/>
11        
12</aura:component>
13

クライアント側コントローラのソース

1swfobject.registerObject("clippy.codeblock-1", "9");({
2    throwErrorForKicks: function(cmp) {
3        // this sample always throws an error
4        var hasPerm = false;
5        try {
6            if (!hasPerm) {
7                throw new Error("You don't have permission to edit this record.");
8            }
9        }
10        catch (e) {
11            // config for a dynamic ui:message component
12            var componentConfig = {
13                componentDef : "markup://ui:message",
14                attributes : {
15                    values : {
16                        title : "Sample Thrown Error",
17                        severity : "error",
18                        body : [
19                            {
20                                componentDef : "markup://ui:outputText",
21                                attributes : {
22                                    values : {
23                                        value : e.message
24                                    }
25                                }
26                            }
27                        ]
28                    }
29                }
30            };
31
32            $A.componentService.newComponentAsync(
33                this,
34                function(message) {
35                    var div1 = cmp.find("div1");
36                    
37                    // Replace existing body with the dynamic component
38                    div1.set("v.body", message);
39                },
40                componentConfig
41            );
42            
43            
44
45        }
46    }
47})
48

try-catch ブロックでエラーを返す例については、コントローラのコードを参照してください。エラーのメッセージは、動的に作成される ui:message コンポーネントでユーザに表示されます。