Error handling 如何使用$util.error在AppSync中发送自定义错误
我有一个关于AppSync错误处理的问题。我想将Error handling 如何使用$util.error在AppSync中发送自定义错误,error-handling,graphql,graphql-js,aws-appsync,vtl,Error Handling,Graphql,Graphql Js,Aws Appsync,Vtl,我有一个关于AppSync错误处理的问题。我想将errorInfo对象与错误响应一起发送,并尝试使用$util.error。根据文件: $util.error(字符串、字符串、对象、对象) 抛出一个自定义错误。这可以在请求或响应映射中使用 模板,如果模板检测到请求或 调用结果。此外,errorType字段、数据字段、, 并且可以指定errorInfo字段。将添加数据值 到GraphQL中错误内部的相应错误块 答复。注意:数据将根据查询选择进行过滤 设置errorInfo值将添加到相应的错误中
errorInfo
对象与错误响应一起发送,并尝试使用$util.error
。根据文件:
$util.error(字符串、字符串、对象、对象)
抛出一个自定义错误。这可以在请求或响应映射中使用
模板,如果模板检测到请求或
调用结果。此外,errorType字段、数据字段、,
并且可以指定errorInfo字段。将添加数据值
到GraphQL中错误内部的相应错误块
答复。注意:数据将根据查询选择进行过滤
设置errorInfo值将添加到相应的错误中
阻止GraphQL响应中的内部错误。注意:错误信息将不会显示
将根据查询选择集进行筛选
下面是我的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
结果是我使用了一些教程中的代码,但这些代码不是最新的。分解器映射模板有两个版本:2018-05-29
和2017-02-28
。因此,我需要将模板版本更改为2018-05-29
,以使其正常工作
RequestMappingTemplate: |
{
"version": "2018-05-29",
"operation": "Invoke",
"payload": {
"field": "getError",
"arguments": $utils.toJson($context.arguments)
}
}
请参见此处的两个版本之间的更改:关于errorData的类似问题:但对于errorInfo,这并不能回答此问题。这是用于请求映射。Lambda函数在请求映射之后执行。错误抛出由响应映射捕获。@ShadabFaiz AWS响应映射模板没有版本控制,而是从请求映射模板获取其版本(我找不到任何关于此AWS文档的明确提及,但这是通过:1暗示的。“给定以下响应映射模板….之前与2017-02-28一起使用…”例如,没有版本字段[,2.“所有请求映射模板通用,版本字段定义”[