amazon-web-services - 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)
很明显,确实发生了错误,但它不会触发我的响应模板中的案例。如何为验证错误返回自定义消息?在这种情况下甚至可能吗?
解决方案
根据您提供的信息,您的PutItem
操作似乎确实成功并返回了您放在$ctx.result
现场的物品。然后,当 AppSync 尝试将响应映射模板的输出中的一个字段强制转换为 AWSPhone 时,它会因您提到的验证错误而失败。
将#if($ctx.error) $util.error("Custom error response")
仅捕获 DynamoDB 错误。从结果到字段输出类型的 GraphQL 强制发生在模板评估之后。
解决此问题的一种方法是将验证添加到您的请求映射模板中,然后再将其保存到 DynamoDB 中,或者将来自 DynamoDB 的值更改为正确的 AWSPhone 标量格式。
推荐阅读
- vue.js - Vue 可拖动克隆并拖动事件
- yaml - 由于列表键,Yq 解析 yaml 失败
- mongodb - 如何使重复的 Mongo 数据库与主数据库保持同步?
- clang-tidy - clang-tidy 中的“AnalyzeTemporaryDtors”选项是什么意思?
- opencv - 将 Gstreamer 启动转换为相机 OV9281 的 OpenCV 管道
- c# - 我的 android 头停止触发断点
- azure - 尝试从 Azure DevOps 管道运行 Power Shell 脚本以连接到 Azure Windows 2016 vm
- javascript - 多次使用 mongo 连接返回无法使用已结束的会话
- javascript - amcharts - 如何在鼠标悬停时加粗文本?
- snowflake-cloud-data-platform - 在雪花中展平嵌套的 JSON