【发布时间】:2020-06-09 13:36:54
【问题描述】:
我正在为 AWS API Gateway 端点构建一个响应模板来处理错误。传入的 JSON 错误消息类似于以下内容之一:
{
"__type": "UserNotFoundException",
"message": "User does not exist."
}
{
"__type": "NotAuthorizedException",
"message": "Incorrect username or password."
}
{
"__type": "NotAuthorizedException",
"message": "Password attempts exceeded"
}
我的 VTL 映射模板如下所示:
#set($inputRoot = $input.path('$'))
#set($unf = "UserNotFoundException")
#set($nae = "NotAuthorizedException")
#set($type =$input.json('$.__type'))
#if($type == $unf)
{
"__type": "UserNotFoundException",
"message": $input.json('$.message'),
"referenceCode": "UNF0000"
}
#elseif($type == $nae)
{
"__type": "NotAuthorizedException",
"message": $input.json('$.message'),
"referenceCode": "NAE0000"
}
#else
{
"__type": $input.json('$.__type'),
"message": $input.json('$.message'),
"referenceCode": "FAIL0000"
}
#end
无论我使用什么输入来触发我的 400 错误响应,它都属于我的万能情况。在我的 else 案例中输出的 __type 与其他条件之一匹配,所以我很困惑为什么他们没有抓住它。任何帮助,将不胜感激! (我是 AWS 和 VTL 的新手)
【问题讨论】:
标签: json aws-api-gateway velocity