首页 > 解决方案 > AppSync 验证错误不会触发响应映射模板中的错误处理程序

问题描述

我正在使用 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-servicesamazon-dynamodbaws-appsync

解决方案


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

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

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


推荐阅读