Receive detailed error messages by checking the AWS API Gateway Validator

Background

I have an API gateway created using Swagger 2.0 definitions with API gateway extensions .

I tried the answers of the default API gateway, for example:

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

The above payload $contextreceives the API gateway variables .

The sample resource / method in my API looks like this (always 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

With the appropriate definition of the request payload:

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

And request validator extensions :

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

Problem

When I call this endpoint with a missing or invalid date, I always get the same answer:

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

However, when I test it through the Gateway API console without a property date:

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, .

+20
4

UPDATE:

. BAD_REQUEST_BODY [docs] (https://docs.aws.amazon.com/apigateway/latest/developerguide/supported-gateway-response-types.html)

{"message":$context.error.validationErrorString}

( API Gateway)

, . , , .

+14

API Gateway , , , , !

, cloudwatch API-, , .

, Request Validator

aws. Amazon CloudWatch API, Amazon API Gateway? .

API Gateway Lambda Logs, .

API- , . enter image description here

API , , :

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

enter image description here

, , Invalid request body , {"message": "Internal server error"}. , , API-.

+4

:

Bad Request Body [400]

Change the value of the body mapping template to: 

{"message":$context.error.validationErrorString}

Ex Output:

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

, , , ( ).

API API Gateway ( Swagger). ( 60+ , REST, REST ).

API Gateway , ( serverless).

, "" . REST GraphQL.

, Lambda ( GraphQL ).

To test the query, it comes free with GraphQL (another reason to love GraphQL). As for our REST handler, we use JSON schemes and any validation error, we can easily return to the client with the HTTP 400 error message.

+2
source

Source: https://habr.com/ru/post/1693429/


All Articles