【问题标题】:AppSync validation error doesn't trigger error handler in response mapping templateAppSync 验证错误不会触发响应映射模板中的错误处理程序
【发布时间】:2019-10-22 02:35:48
【问题描述】:

我正在使用 AppSync,并且似乎验证错误不会触发响应映射模板中的错误捕获。 属性值包含 AWSPhone 输入。 如果我为 AWSPhone 输入了错误的格式,错误(如预期)是:

{
  "data": null,
  "errors": [
    {
      "path": null,
      "locations": [
        {
          "line": 2,
          "column": 17,
          "sourceName": null
        }
      ],
      "message": "Validation error of type WrongType: argument 'input.company.phoneNumber' with value 'StringValue{value='+1-541-754-300'}' is not a valid 'AWSPhone' @ 'createProfile'"
    }
  ]
}

我的请求映射模板是这样的:

{
  "version": "2018-05-29",
  "operation": "PutItem",
  "key": {
    "id": $util.dynamodb.toDynamoDBJson($ctx.args.input.client),
  },
  "attributeValues": $util.dynamodb.toMapValuesJson($ctx.args.input),
}

我的响应映射模板:

#if($ctx.error)
  $util.error("Custom error response")
#end
  $util.toJson($ctx.result)

很明显确实发生了错误,但它不会触发我的响应模板中的案例。 如何为验证错误返回自定义消息? 在这种情况下甚至可能吗?

【问题讨论】:

    标签: amazon-web-services amazon-dynamodb aws-appsync


    【解决方案1】:

    根据您提供的内容,您的PutItem 操作似乎确实成功并返回了您放在$ctx.result 字段中的项目。然后,当 AppSync 尝试将响应映射模板的输出中的一个字段强制转换为 AWSPhone 时,它​​会因您提到的验证错误而失败。

    #if($ctx.error) $util.error("Custom error response") 将仅捕获 DynamoDB 错误。从结果到字段输出类型的 GraphQL 强制发生在模板评估之后

    解决此问题的一种方法是在将请求映射模板保存到 DynamoDB 之前将验证添加到您的请求映射模板中,或者将来自 DynamoDB 的值更改为正确的 AWSPhone 标量格式。

    【讨论】:

      猜你喜欢
      • 2022-01-23
      • 2019-07-19
      • 1970-01-01
      • 1970-01-01
      • 2012-02-06
      • 2016-01-30
      • 2014-07-01
      • 2013-05-18
      • 2021-11-04
      相关资源
      最近更新 更多