簡體   English   中英

從 AWS API 網關請求驗證器獲取詳細的錯誤消息

[英]Get detailed error messages from AWS API Gateway Request Validator

背景

我有一個 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"
          }
        }

上面 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

以及請求驗證器擴展

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映射,但到目前為止我還沒能做到。

更新:

這現在可以通過網關響應實現。 使用以下模板設置BAD_REQUEST_BODY網關響應:

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

(API 網關上的開發人員)

不幸的是,目前不支持此功能。 我們正在積極致力於解決這個問題,但我不能給你任何具體的時間表,什么時候可以得到支持。

既然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}

在此處輸入圖片說明

它比您作為Invalid request body和其他人擁有的信息包含更多詳細信息,例如{"message": "Internal server error"} 這是一個非常有用的功能,它為我節省了很多時間來解決無服務器和 api 網關問題。

在這種情況下,在網關響應部分中,轉到:

Bad Request Body [400]

Change the value of the body mapping template to: 

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

防爆輸出:

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

這不是您問題的答案,而是我們在我們的應用程序中使用的替代解決方法,用於相同目的(請求驗證)。

我們的無服務器 API 首先定義了 API Gateway 中的所有端點(完整的 Swagger 文檔)。 隨着時間的推移,我們添加了更多端點(大約 60 多個端點,包括遺留 REST 端點、公共 REST 端點和私有 graphQL 端點)。

事實證明,通過 API Gateway 管理這么多端點非常繁瑣,部署時間也很長(我們使用的是serverless )。

最終,我們決定將其縮減為三個“單體”無服務器應用程序。 兩個 REST 端點和一個 GraphQL 端點。

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

對於請求驗證,它隨 GraphQL 免費提供(另一個喜歡 GraphQL 的原因)。 至於我們的 REST 處理程序,我們使用 JSON 模式和任何驗證錯誤,我們可以輕松地將 HTTP 400 錯誤消息連同 HTTP 400 錯誤消息一起返回給客戶端。

如果您在 Cloudformation 或 SAM 中執行此操作,請添加以下內容:

SomeAPI:
  Type: 'AWS::Serverless::Api'
  Properties:
    GatewayResponses:
      BAD_REQUEST_BODY:
        ResponseTemplates:
          "application/json": '{ "message": "$context.error.validationErrorString"}'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM