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

Tan*_*ong 7 error-handling vtl graphql graphql-js aws-appsync

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

\n\n

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

\n\n
\n

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

\n\n

抛出自定义错误。如果模板检测到请求或调用结果有错误,则可以在请求或响应映射模板中使用该模板。此外,还可以指定 errorType 字段、data 字段和 errorInfo 字段。数据值将添加到 GraphQL 响应中错误内的相应错误块中。注意:数据将根据查询选择集进行过滤。errorInfo 值将添加到 GraphQL 响应中错误内相应的 error\n 块中。注意:errorInfo 将不会根据查询选择集进行过滤。

\n
\n\n

这是我的 ResponseMappingTemplate 的样子:

\n\n
#if( $context.result && $context.result.errorMessage )\n  $utils.error($context.result.errorMessage, $context.result.errorType, $context.result.data), $context.result.errorInfo)\n#else\n  $utils.toJson($context.result.data)\n#end\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n\n
var result = {\n  data: null,\n  errorMessage: \'I made this error\',\n  errorType: \'ALWAYS_ERROR\',\n  errorInfo: {\n    errorCode: 500,\n    validations: [\n      {\n        fieldName: \'_\',\n        result: false,\n        reasons: [\n          \'Failed! Yay!\'\n        ]\n      }\n    ],\n  }\n};\ncallback(null, result);\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

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

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

\n\n
{\n  "data": {\n    "getError": null\n  },\n  "errors": [\n    {\n      "path": [\n        "getError"\n      ],\n      "data": null,\n      "errorType": "ALWAYS_ERROR",\n      "errorInfo": null,\n      "locations": [\n        {\n          "line": 2,\n          "column": 3,\n          "sourceName": null\n        }\n      ],\n      "message": "I made this error"\n    }\n  ]\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n

Tan*_*ong 6

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

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

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