

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 错误处理（错误操作）
<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  
触发错误操作的规则的名称。

topic  
收到原始消息的主题。

cloudwatchTraceId  
引用错误的唯一身份登录 CloudWatch。

clientId  
消息发布程序的客户端 ID。

base64 OriginalPayload  
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-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)。