2

I am setting up an endpoint for a lambda in api-gateway using openapi. This lambda will need to be be able to return 400/500 error codes for internal errors/bad requests. To this end I have tried to format the x-amazon-apigateway-integration section of the openapi json document for this endpoint as follows:

"x-amazon-apigateway-integration": {
    ...
    "responses": {
        ".*statusCode\\\":400.*": {
            "statusCode": "400",
            "responseTemplates": {
                "application/json": "#set ($errorMessageObj = $util.parseJson($input.path('$.errorMessage'))) \n{ \"message\" : \"$errorMessageObj.message\"}"
            }
        }
    }
}

The raw output from my lambda (i.e. before transformations) looks like this:

"{\"statusCode\":400,\"message\":\"Invalid request parameters\"}"

My expectation would be that the final response would have an error code of 400 and the object returned to my client would be

{
    "message": "Invalid request parameters"
}

But I am getting the full untransformed response object back with a response code of 200 (the default). I have referenced this aws-blog-post which towards the bottom has the regex I am attempting to use. It passes a regex-parser So I am not understanding why I am still getting the default response back. I am using openapi v3. What is is that I am missing here?

1 Answers1

0

I figured it out. In the lambda you need to throw an error with the response code you want parsed out. If you simply return it assumes correct execution.