相关疑难解决方法(0)

如何使用 $util.error 在 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 …
Run Code Online (Sandbox Code Playgroud)

error-handling vtl graphql graphql-js aws-appsync

7
推荐指数
1
解决办法
1万
查看次数

标签 统计

aws-appsync ×1

error-handling ×1

graphql ×1

graphql-js ×1

vtl ×1