

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 錯誤處理 (錯誤動作)
<a name="rule-error-handling"></a>

當 AWS IoT 收到來自裝置的訊息時，規則引擎會檢查訊息是否符合規則。若是如此，則會對規則的查詢陳述式進行評估，並啟動規則的動作，傳送查詢陳述式的結果。

如果在啟動動作時發生問題，則若針對規則指定了錯誤動作，規則引擎便會啟動該動作。這可能在以下情況時發生：
+ 規則並未擁有存取 Amazon S3 儲存貯體的許可。
+ 使用者錯誤會造成 DynamoDB 佈建的傳輸量超量。

**注意**  
本主題所述的錯誤處理適用於[規則動作](iot-rule-actions.md)。若要偵錯 SQL 問題，包括外部函數，您可以設定 AWS IoT 記錄。如需詳細資訊，請參閱[設定 AWS IoT 記錄](configure-logging.md)。

## 錯誤動作訊息格式
<a name="rule-error-message-format"></a>

每一項規則和訊息會產生單條訊息。例如，如果相同規則中的兩個規則動作失敗，則錯誤動作會收到包含那兩個錯誤的訊息。

錯誤動作訊息看似下列範例。

```
{
  "ruleName": "TestAction",
  "topic": "testme/action",
  "cloudwatchTraceId": "7e146a2c-95b5-6caf-98b9-50e3969734c7",
  "clientId": "iotconsole-1511213971966-0",
  "base64OriginalPayload": "ewogICJtZXNzYWdlIjogIkhlbGxvIHZyb20gQVdTIElvVCBjb25zb2xlIgp9",
  "failures": [
    {
      "failedAction": "S3Action",
      "failedResource": "us-east-1-s3-verify-user",
      "errorMessage": "Failed to put S3 object. The error received was The specified bucket does not exist (Service: Amazon S3; Status Code: 404; Error Code: NoSuchBucket; Request ID: 9DF5416B9B47B9AF; S3 Extended Request ID: yMah1cwPhqTH267QLPhTKeVPKJB8BO5ndBHzOmWtxLTM6uAvwYYuqieAKyb6qRPTxP1tHXCoR4Y=). Message arrived on: error/action, Action: s3, Bucket: us-east-1-s3-verify-user, Key: \"aaa\". Value of x-amz-id-2: yMah1cwPhqTH267QLPhTKeVPKJB8BO5ndBHzOmWtxLTM6uAvwYYuqieAKyb6qRPTxP1tHXCoR4Y="
    }
  ]
}
```

ruleName  
觸發錯誤動作的規則名稱。

主題  
收到原始訊息的主題。

cloudwatchTraceId  
指向 CloudWatch 中錯誤記錄的專屬識別資料。

clientId  
訊息發佈者的用戶端 ID。

base64OriginalPayload  
原始訊息承載 Base64 編碼。

failures    
failedAction  
無法完成的動作名稱 (例如，「S3Action」)。  
failedResource  
資源名稱 (例如，S3 儲存貯體名稱)。  
errorMessage  
對該項錯誤的敘述和說明。

## 錯誤動作範例
<a name="rule-error-example"></a>

以下範例為有加入錯誤動作的規則。下列規則中有個動作會將訊息資料寫入 DynamoDB 表格，還有一個錯誤動作，會將資料寫入 Amazon S3 儲存貯體：

```
{
    "sql" : "SELECT * FROM ..."
    "actions" : [{ 
        "dynamoDB" : {
            "table" : "PoorlyConfiguredTable",
            "hashKeyField" : "AConstantString",
            "hashKeyValue" : "AHashKey"}}
    ],
    "errorAction" : { 
        "s3" : {
            "roleArn": "arn:aws:iam::123456789012:role/aws_iot_s3",
            "bucketName" : "message-processing-errors",
            "key" : "${replace(topic(), '/', '-') + '-' + timestamp() + '-' + newuuid()}"
        }
    }
}
```

您可以在錯誤動作的 SQL 陳述式中使用任何[函數](iot-sql-functions.md)或[替代範本](https://docs.aws.amazon.com//iot/latest/developerguide/iot-substitution-templates.html)，包括外部函數：[https://docs.aws.amazon.com//iot/latest/developerguide/iot-sql-functions.html#iot-func-aws-lambda](https://docs.aws.amazon.com//iot/latest/developerguide/iot-sql-functions.html#iot-func-aws-lambda)、[https://docs.aws.amazon.com//iot/latest/developerguide/iot-sql-functions.html#iot-sql-function-get-dynamodb](https://docs.aws.amazon.com//iot/latest/developerguide/iot-sql-functions.html#iot-sql-function-get-dynamodb)、[https://docs.aws.amazon.com//iot/latest/developerguide/iot-sql-functions.html#iot-sql-function-get-registry_data](https://docs.aws.amazon.com//iot/latest/developerguide/iot-sql-functions.html#iot-sql-function-get-registry_data)、[https://docs.aws.amazon.com//iot/latest/developerguide/iot-sql-functions.html#iot-sql-function-get-thing-shadow](https://docs.aws.amazon.com//iot/latest/developerguide/iot-sql-functions.html#iot-sql-function-get-thing-shadow)[https://docs.aws.amazon.com//iot/latest/developerguide/iot-sql-functions.html#iot-sql-function-get-secret](https://docs.aws.amazon.com//iot/latest/developerguide/iot-sql-functions.html#iot-sql-function-get-secret)、 [https://docs.aws.amazon.com//iot/latest/developerguide/iot-sql-functions.html#iot-sql-function-machine-learning](https://docs.aws.amazon.com//iot/latest/developerguide/iot-sql-functions.html#iot-sql-function-machine-learning)和 [https://docs.aws.amazon.com//iot/latest/developerguide/iot-sql-functions.html#iot-sql-decode-base64](https://docs.aws.amazon.com//iot/latest/developerguide/iot-sql-functions.html#iot-sql-decode-base64)。如果錯誤動作需要呼叫外部 函數，則叫用錯誤動作可能會導致外部函數產生額外的帳單。

如需規則以及如何指定錯誤動作的詳細資訊，請參閱[建立 AWS IoT 規則](https://docs.aws.amazon.com//iot/latest/developerguide/iot-create-rule.html)。

若需更多有關使用 CloudWatch 監控規則成功或失敗的資訊，請參閱 [AWS IoT 指標和維度](metrics_dimensions.md)。