【问题标题】:Get detailed error messages from AWS API Gateway Request Validator从 AWS API Gateway 请求验证器获取详细的错误消息
【发布时间】:2017-12-23 15:08:59
【问题描述】:

背景

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

我覆盖了默认的 API Gateway 响应,例如:

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 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 Gateway 控制台对其进行测试时:

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


    【解决方案1】:

    更新:

    这现在可以通过网关响应来实现。使用以下模板设置BAD_REQUEST_BODY 网关响应:

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

    (API 网关开发者)

    很遗憾,目前不支持此功能。我们正在积极努力解决此问题,但我无法为您提供任何具体的时间表,直到何时可以支持。

    【讨论】:

    • CORS 验证错误是否与您的回复有关? stackoverflow.com/q/48710640/1480391
    • 如何使用无服务器框架实现这一点?
    • @ShwetaJ 我不了解无服务器,但在 aws sam 中,您可以使用 Gateway Responses property,如下所示:{ "GatewayResponses": { "DEFAULT_4xx": { "ResponseParameters": { "Headers": { "Access-Control-Allow-Origin": "'*'" } }, "ResponseTemplates": { "application/json": "{\"message\": \"$context.error.validationErrorString\"}" } } } }
    【解决方案2】:

    既然 API Gateway 开发者已经回答了这个问题,我仍然想为您添加一些提示,也许它会有所帮助并且可以被接受!

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

    如果它包含Request Validator 的详细信息,请告诉我

    aws document - How do I enable Amazon CloudWatch Logs for APIs I created in the Amazon API Gateway? 提供了如何启用它的步骤。

    但我更喜欢使用此文档API Gateway and Lambda Logs,它提供了屏幕截图以便轻松跟进。

    在您的 api 网关中,您应该会看到它已启用。

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

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

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

    【讨论】:

      【解决方案3】:

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

      Bad Request Body [400]
      
      Change the value of the body mapping template to: 
      
      {"message":$context.error.validationErrorString}
      

      Ex 输出:

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

      【讨论】:

      • 问题是关于 Swagger,而不是 Amazon Console。请更新您的答案,因为它只是它的一半。
      【解决方案4】:

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

      我们的无服务器 API 从在 API Gateway 中定义所有端点开始(包含 Swagger 文档)。随着时间的推移,我们添加了更多端点(大约 60 多个端点,包括传统 REST 端点、公共 REST 端点和私有 graphQL 端点)。

      事实证明,通过 API Gateway 管理这么多端点非常繁琐,部署时间也很长(我们使用的是serverless)。

      最终,我们决定将其简化为三个“单体”无服务器应用程序。两个 REST 端点和一个 GraphQL 端点。

      所以基本上,我们在 Lambda 处理程序中处理了路由(GraphQL 不需要路由)。

      对于请求验证,GraphQL 免费提供(喜欢 GraphQL 的另一个原因)。至于我们的 REST 处理程序,我们使用 JSON 模式和任何验证错误,我们可以轻松地连同 HTTP 400 错误消息一起返回给客户端。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-09-11
        • 2016-08-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-11
        • 2018-07-20
        相关资源
        最近更新 更多