从AWS API网关请求验证器获取详细的错误消息

问题描述 投票:20回答:4

背景

我有一个使用API Gateway extensions的Swagger 2.0定义创建的API网关。

例如,我取代了默认的API网关响应:

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"
          }
        }

上述有效载荷中的$context来自API Gateway variables

我的API中的示例资源/方法如下(总是LAMBDA_PROXY集成):

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

带有相应的请求有效负载定义:

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

request validator extensions

x-amazon-apigateway-request-validator: body
x-amazon-apigateway-request-validators:
  body:
    validateRequestBody: true
    validateRequestParameters: false

问题

当我用缺少或无效的date呼叫此端点时,我总是得到相同的响应:

{
    "error": {
        "code": 400,
        "stage": "latest",
        "request": "6b7a64f5-e7f0-11e7-845b-f53ceb4cb049",
        "message": "Invalid request body"
    }
}

但是,当我通过不带date属性的API网关控制台进行测试时:

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映射,但到目前为止我还没有做到这一点。

amazon-web-services swagger aws-api-gateway
4个回答
16
投票

更新:

现在可以通过网关响应来实现。使用以下模板(x-amazon-apigateway-gateway-responses

设置网关响应BAD_REQUEST_BODY [docs]
https://docs.aws.amazon.com/apigateway/latest/developerguide/supported-gateway-response-types.html

(API网关上的开发人员)

很遗憾,目前不支持此功能。我们正在积极致力于解决此问题,但在何时可以支持的情况下,我无法提供具体的时间表。


4
投票

由于API Gateway开发人员已经回答了这个问题,我仍然想为您添加一些技巧,也许有帮助,并且可以被接受!

对于您的问题,实际上,您需要为api网关激活cloudwatch日志,这样,您可以获得比以前更多的日志。

让我知道它是否包含{"message":$context.error.validationErrorString} 的详细信息>

Request Validator提供了如何启用它的步骤。

但是我更喜欢使用本文档aws document - How do I enable Amazon CloudWatch Logs for APIs I created in the Amazon API Gateway?,该文档使屏幕截图易于跟进。

在您的api网关中,您应该看到已启用。API Gateway and Lambda Logs

多次访问API网关,遍历名为:的日志组:

enter image description here

API-Gateway-Execution-Logs_{rest-api-id}/{stage_name}

[比您拥有enter image description here和其他信息,例如Invalid request body更多的信息。这是非常有用的功能,为我节省了很多时间来解决无服务器和API网关问题。


3
投票

在这种情况下,在“网关响应”部分中,转到:


2
投票

这不是您问题的答案,而是我们在应用中使用的替代解决方法,具有相同的目的(请求验证)。

© www.soinside.com 2019 - 2024. All rights reserved.