如何使用 $util.error 在 AppSync 中发送自定义错误

How to send custom error in AppSync with $util.error

我对 AppSync 错误处理有疑问。我想发送 errorInfo 对象以及错误响应,我尝试使用 $util.error。根据文档:

https://docs.aws.amazon.com/appsync/latest/devguide/resolver-util-reference.html

$util.error(String, String, Object, Object)

Throws a custom error. This can be used in request or response mapping templates if the template detects an error with the request or with the invocation result. Additionally, an errorType field, a data field, and a errorInfo field can be specified. The data value will be added to the corresponding error block inside errors in the GraphQL response. Note: data will be filtered based on the query selection set. The errorInfo value will be added to the corresponding error block inside errors in the GraphQL response. Note: errorInfo will NOT be filtered based on the query selection set.

这是我的 ResponseMappingTemplate 的样子:

#if( $context.result && $context.result.errorMessage )
  $utils.error($context.result.errorMessage, $context.result.errorType, $context.result.data), $context.result.errorInfo)
#else
  $utils.toJson($context.result.data)
#end

这是我在解析器上所做的:

var result = {
  data: null,
  errorMessage: 'I made this error',
  errorType: 'ALWAYS_ERROR',
  errorInfo: {
    errorCode: 500,
    validations: [
      {
        fieldName: '_',
        result: false,
        reasons: [
          'Failed! Yay!'
        ]
      }
    ],
  }
};
callback(null, result);

这是我在 CloudWatch 日志中看到的内容:

{
    "errors": [
        "CustomTemplateException(message=I made this error, errorType=ALWAYS_ERROR, data=null, errorInfo={errorCode=500, validations=[{fieldName=_, result=false, reasons=[Failed! Yay!]}]})"
    ],
    "mappingTemplateType": "Response Mapping",
    "path": "[getError]",
    "resolverArn": "arn:aws:appsync:ap-southeast-1:....",
    "context": {
        "arguments": {},
        "result": {
            "errorMessage": "I made this error",
            "errorType": "ALWAYS_ERROR",
            "errorInfo": {
                "errorCode": 500,
                "validations": [
                    {
                        "fieldName": "_",
                        "result": false,
                        "reasons": [
                            "Failed! Yay!"
                        ]
                    }
                ]
            }
        },
        "stash": {},
        "outErrors": []
    },
    "fieldInError": true
}

这是我在回复中得到的内容:

{
  "data": {
    "getError": null
  },
  "errors": [
    {
      "path": [
        "getError"
      ],
      "data": null,
      "errorType": "ALWAYS_ERROR",
      "errorInfo": null,
      "locations": [
        {
          "line": 2,
          "column": 3,
          "sourceName": null
        }
      ],
      "message": "I made this error"
    }
  ]
}

请注意 errorInfo 为空,我知道如何得到 CustomTemplateException。我怀疑这是因为 $utils.error 的第 4 个参数。但我不知道为什么。谁能帮忙指出错误或告诉我是否可以发送自定义 errorInfo

原来我使用了一些不是最新的教程中的代码。解析器映射模板有 2 个版本:2018-05-292017-02-28。所以我需要将模板版本更改为 2018-05-29 才能正常工作。

RequestMappingTemplate: |
  {
    "version": "2018-05-29",
    "operation": "Invoke",
    "payload": {
      "field": "getError",
      "arguments":  $utils.toJson($context.arguments)
    }
  }

在此处查看两个版本之间的变化:https://docs.aws.amazon.com/appsync/latest/devguide/resolver-mapping-template-changelog.html#changing-the-version-on-a-function