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

Ali*_*xel 20 amazon-web-services swagger aws-api-gateway

背景

我有一个使用带有API网关扩展的 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"
          }
        }
Run Code Online (Sandbox Code Playgroud)

$context上述有效载荷来自API网关变量.

我的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
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

请求验证器扩展:

x-amazon-apigateway-request-validator: body
x-amazon-apigateway-request-validators:
  body:
    validateRequestBody: true
    validateRequestParameters: false
Run Code Online (Sandbox Code Playgroud)

问题

当我将此端点称为缺失或无效时date,我总是得到相同的响应:

{
    "error": {
        "code": 400,
        "stage": "latest",
        "request": "6b7a64f5-e7f0-11e7-845b-f53ceb4cb049",
        "message": "Invalid request body"
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,当我在没有date属性的情况下通过API网关控制台进行测试时:

Request body does not match model schema for content type application/json: [
  object has missing required properties (["date"])
]
Run Code Online (Sandbox Code Playgroud)

并且无效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"
]
Run Code Online (Sandbox Code Playgroud)

如何访问详细的错误消息,以便我可以使用更具描述性的消息来丰富我的错误响应Invalid request body?我怀疑这一定是可能的,也许是使用x-amazon-apigateway-gateway-responses映射,但到目前为止我还没能做到.

Abh*_*aja 16

(API网关上的开发人员)

不幸的是,现在不支持此功能.我们正在积极致力于解决此问题,但我无法在何时支持此问题时向您提供任何具体时间表.

  • 验证错误的 CORS 是否与您的响应有关?/sf/ask/3409744831/ (2认同)

BMW*_*BMW 5

既然API网关开发者已经回答了这个问题,我还是想给你补充一些提示,也许它会有所帮助,并且可以成为一个可以接受的答案!

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

让我知道它是否包含详细信息 Request Validator

aws 文档 - 如何为我在 Amazon API Gateway 中创建的 API 启用 Amazon CloudWatch Logs?给出了如何启用它的步骤。

但我更喜欢使用此文档API Gateway 和 Lambda Logs,它们提供了易于跟进的屏幕截图。

在您的 api 网关中,您应该看到它已启用。 在此处输入图片说明

多次访问API网关,通过名为的日志组:

API-Gateway-Execution-Logs_{rest-api-id}/{stage_name}
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

它比您拥有的信息Invalid request body和其他信息包含更多详细信息,例如{"message": "Internal server error"}. 这是一个非常有用的功能,它为我节省了很多时间来解决无服务器和 api 网关问题。


小智 5

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

Bad Request Body [400]

Change the value of the body mapping template to: 

{"message":$context.error.validationErrorString}
Run Code Online (Sandbox Code Playgroud)

防爆输出:

{
"message": "[instance value (\"us\") not found in enum (possible values: [\"usd\",\"eur\",\"gbp\",\"jpy\",\"aud\",\"chf\",\"cad\",\"nzd\"])]"
}
Run Code Online (Sandbox Code Playgroud)