Lambda error regex failing to detecting Lambda Response body

0

Hi team, I have an API gateway connected to a Lambda function

The response from Lambda is:

{"statusCode":400,"body":"{\"errorType\":\"UserLambdaValidationException\",\"err\":\"PreSignUp failed with error [UserAlreadyExists] Registration failed. A user with that email already exists..\"}"}

I have tried a variety of different Lambda error regex combinations, with the current attempt being:

.*"statusCode"\s*:\s*400.*

Any advice or support would be greatly appreciated.

Here is my configuration in code:

      "post" : {
        "produces" : [ "application/json" ],
        "responses" : {
          "200" : {
            "description" : "200 response",
            "schema" : {
              "$ref" : "#/definitions/Empty"
            },
            "headers" : {
              "Access-Control-Allow-Origin" : {
                "type" : "string"
              }
            }
          },
          "400" : {
            "description" : "400 response",
            "schema" : {
              "$ref" : "#/definitions/Empty"
            },
            "headers" : {
              "Access-Control-Allow-Origin" : {
                "type" : "string"
              }
            }
          }
        },
        "x-amazon-apigateway-integration" : {
          "httpMethod" : "POST",
          "uri" : "arn:aws:apigateway:ap-southeast-2:lambda:path/2015-03-31/functions/<secret_stuff>/invocations",
          "responses" : {
            ".*\"statusCode\"\\s*:\\s*400.*" : {
              "statusCode" : "400",
              "responseParameters" : {
                "method.response.header.Access-Control-Allow-Origin" : "'*'"
              },
              "responseTemplates" : {
                "application/json" : "#set($inputRoot = $input.path('$'))\r\n{\r\n    \"statusCode\": \"$inputRoot.statusCode\",\r\n    \"body\": \"$inputRoot.body\"\r\n}\r\n"
              }
            }
          },
          "passthroughBehavior" : "when_no_match",
          "contentHandling" : "CONVERT_TO_TEXT",
          "type" : "aws"
        }
      },
1 Answer
0

Hi,

It seems that you want to trap errors returned by your application at the API GTW level.

The right way to do it is described here: https://docs.aws.amazon.com/apigateway/latest/developerguide/handle-errors-in-lambda-integration.html

For what you're after (selection pattern), it's described in this section of the above link

To map the standard Lambda error to a method response, you must first decide on 
an HTTP status code for a given Lambda error. You then set a regular expression pattern 
on the selectionPattern property of the IntegrationResponse associated with the given 
HTTP status code. In the API Gateway console, this selectionPattern is denoted as Lambda 
error regex in the Integration response section, under each integration response.

Note
API Gateway uses Java pattern-style regexes for response mapping. For more information, 
see Pattern in the Oracle documentation.

Best,

Didier

profile pictureAWS
EXPERT
answered 9 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions