从 AWS API Gateway 请求验证器获取详细的错误消息 [英] Get detailed error messages from AWS API Gateway Request Validator
问题描述
我有一个使用 Swagger 2.0 定义和 API 网关扩展.
I have an API Gateway created using Swagger 2.0 definitions with API Gateway extensions.
我覆盖了默认的 API 网关响应,例如:
I overrode the default API Gateway responses, for instance:
x-amazon-apigateway-gateway-responses:
BAD_REQUEST_BODY:
statusCode: 400
responseTemplates:
application/json: |
{
"error": {
"code": 400,
"stage": "$context.stage",
"request": "$context.requestId",
"message": "$context.error.message"
}
}
上述payload中的$context
来自API 网关变量.
The $context
in the above payload comes from API Gateway variables.
我的 API 中的示例资源/方法如下所示(总是 LAMBDA_PROXY
集成):
A sample resource/method in my API looks like this (always LAMBDA_PROXY
integrations):
paths:
/test:
post:
parameters:
- in: body
name: Test
required: true
schema:
$ref: "#/definitions/Test"
responses:
201:
description: Created
400:
description: Bad Request
401:
description: Unauthorized
403:
description: Forbidden
x-amazon-apigateway-integration:
uri: >-
arn:aws:apigateway:${region}:lambda:path/2015-03-31/functions/${lambda}/invocations
type: aws_proxy
httpMethod: POST
credentials: "${credentials}"
passthroughBehavior: never
带有相应的请求负载定义:
With the corresponding request payload definition:
definitions:
Test:
type: object
title: Test
required:
- date
properties:
date:
type: string
pattern: "^20[0-9]{2}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])$"
description: Date in YYYY-MM-DD Format
以及 请求验证器扩展:
x-amazon-apigateway-request-validator: body
x-amazon-apigateway-request-validators:
body:
validateRequestBody: true
validateRequestParameters: false
问题
当我使用缺失或无效的date
调用此端点时,我总是得到相同的响应:
Problem
When I call this endpoint with a missing or invalid date
, I always get the same response:
{
"error": {
"code": 400,
"stage": "latest",
"request": "6b7a64f5-e7f0-11e7-845b-f53ceb4cb049",
"message": "Invalid request body"
}
}
但是,当我通过没有 date
属性的 API Gateway 控制台对其进行测试时:
However, when I test it through the API Gateway console without the date
property:
Request body does not match model schema for content type application/json: [
object has missing required properties (["date"])
]
并且带有无效的date
:
Request body does not match model schema for content type application/json: [
ECMA 262 regex "^20[0-9]{2}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])$" does not match input string "2017/12/25"
]
问题
如何访问详细的错误消息,以便我可以使用比 Invalid request body
更具描述性的消息来丰富我的错误响应?我怀疑这一定是可能的,也许使用 x-amazon-apigateway-gateway-responses
映射,但到目前为止我还没有做到.
Question
How can I access the detailed error message so that I can enrich my error response with a more descriptive message than Invalid request body
? I suspect that this must be possible, perhaps using x-amazon-apigateway-gateway-responses
mapping, but so far I haven't been able to do it.
推荐答案
更新:
现在可以通过网关响应来实现.设置 BAD_REQUEST_BODY
使用以下模板的网关响应:
This is now possible with gateway responses. Setup a BAD_REQUEST_BODY
gateway response with the below template:
{"message": "$context.error.validationErrorString"}
<小时>
(API 网关上的开发人员)
(Developer on API Gateway)
很遗憾,目前不支持此功能.我们正在积极致力于解决此问题,但我无法向您提供任何具体的时间表,直到何时可以得到支持.
Unfortunately, this is not supported right now. We are actively working on fixing this issue, but I can't give you any specific timelines by when this could be supported.
这篇关于从 AWS API Gateway 请求验证器获取详细的错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!