我对 AppSync 错误处理有疑问。我想发送errorInfo对象以及错误响应,我尝试了$util.error. 根据文件:
https://docs.aws.amazon.com/appsync/latest/devguide/resolver-util-reference.html
\n\n\n\n\n\n\n
$util.error(String, String, Object, Object)抛出自定义错误。如果模板检测到请求或调用结果有错误,则可以在请求或响应映射模板中使用该模板。此外,还可以指定 errorType 字段、data 字段和 errorInfo 字段。数据值将添加到 GraphQL 响应中错误内的相应错误块中。注意:数据将根据查询选择集进行过滤。errorInfo 值将添加到 GraphQL 响应中错误内相应的 error\n 块中。注意:errorInfo 将不会根据查询选择集进行过滤。
\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\nRun Code Online (Sandbox Code Playgroud)\n\n这是我在解析器上所做的:
\n\nvar 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)