gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 07:38:40 24 4
gpt4 key购买 nike

我有一个关于 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 的第四个参数.但我不知道为什么。谁能帮忙指出错误或告诉我是否发送自定义 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)
}
}

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

关于error-handling - 如何使用 $util.error 在 AppSync 中发送自定义错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53493889/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com