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

Posted

技术标签:

【中文标题】如何使用 $util.error 在 AppSync 中发送自定义错误【英文标题】:How to send custom error in AppSync with $util.error 【发布时间】:2019-04-28 20:27:22 【问题描述】:

我有一个关于 AppSync 错误处理的问题。我想将errorInfo 对象连同错误响应一起发送,我尝试使用$util.error。根据文档:

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

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

引发自定义错误。这可用于请求或响应映射 如果模板检测到请求错误或 调用结果。此外,errorType 字段、数据字段、 并且可以指定 errorInfo 字段。将添加数据值 到 GraphQL 中的错误中的相应错误块 回复。注意:数据将根据查询选择进行过滤 放。 errorInfo 值将添加到相应的错误中 阻止 GraphQL 响应中的内部错误。注意:errorInfo 不会 根据查询选择集进行过滤。

这是我的 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

【问题讨论】:

errorData 的类似问题:***.com/questions/51733996/… 但这并不能回答 errorInfo 的这个问题。 【参考方案1】:

原来我使用了一些教程中的代码,但不是最新的。解析器映射模板有 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

【讨论】:

这用于请求映射。 Lambda 函数在请求映射后执行。错误抛出由响应映射捕获。 @ShadabFaiz AWS 响应映射模板没有版本化,而是从请求映射模板中获取它们的版本(我找不到任何明确提及此 AWS 文档,但这是通过以下方式暗示的:1. “给定以下响应映射模板....以前是 2017-02-28...”,没有版本字段 [docs.aws.amazon.com/appsync/latest/devguide/…, 2. “所有请求映射模板通用,版本字段定义”[ docs.aws.amazon.com/appsync/latest/devguide/…

以上是关于如何使用 $util.error 在 AppSync 中发送自定义错误的主要内容,如果未能解决你的问题,请参考以下文章

如何在 iOS 客户端中传递 AWS AppSync 自定义请求标头?

如何在你的库中使用 Spring-Data 以及如何在你的项目中使用这个库?

在 Avkit 中如何使用这三行代码,以及如何将音乐静音”

如何在发布管道中使用输出变量

如何在Hive&Impala中使用UDF

如何使用 Firebase 在 Web 上托管 Flutter?它的效果如何?