Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS API Gateway: Regex for error is not picked up

I have a Lambda function tied to the AWS API gateway. When I call it with something like /foo/bar and bar does not exist, my function returns (correctly) something like:

{
  "code": 404,
  "message": "bar not found"
}

I now want the status code of this to be 404, too, but following the tutorials I could find on the net, I had no success so far. What I did:

I created a method response for 404 that looks like this: 404 response

And in the integration response, I set the following: 404 integration response

To my understanding, this should set the code to 404 when 404 appears in the response document, but I still get 200.

What am I missing?

like image 212
rabejens Avatar asked Nov 26 '17 17:11

rabejens


People also ask

How do I return a custom error object and status code from API gateway with Lambda?

Create a new API Gateway and add a GET method to the root resource. Bind the method to the Lambda you just created and give it the Lambda basic execution role. Navigate to the Method Response for GET and add a 400 Status response. This makes 400 available to assign a regex to in Integration Response.

What is 4xx error in API gateway?

As Marcin said, 4xx errors are typically meaning the client did not make a correct request... you can go into your API Gateway stage and enable the entire request string to be logged for each request... you can then have that to help debug.

What is Lambda error regex?

Our API Gateway's Lambda Error Regex runs a check when throwing an exception in Lambda Function. It does not run on a regular response. Our Lambda Function packs the exception into an object called errorMessage by herself.

Can API gateway handle exceptions?

API Gateway method response and integration response For example, the generated SDKs can unmarshall your API error responses into appropriate exception types which are thrown from the SDK client.


1 Answers

I investigated some more and found out that the API gateway is very picky about where it expects its error messages, and how I have to get them out.

It is deeply buried and not easily found in the documentation that the errorMessage property is considered for matching against the Lambda Error Regex, and the first answer to this post states you have to throw an exception when using Java 8. I use .net for my Lambda function, so I did this very simple thing:

new Exception("404") |> raise

then I set up the Lambda Error Regex 404 and it worked.

Next thing I tried was:

new Exception("foo 404 bar") |> raise

With the regex .*404.* it still worked.

Now comes the thing: I tried to emit a JSON object as the error, but I found nothing in C# or F# to let me do this, so I came up with the following:

type Error = {
    code: int
    message: string
}

...

new Exception({ code = 404; message = name |> sprintf "%s not found" } |> JsonConvert.SerializeObject) |> raise

And boom, I got 200 again.

So I conclude from this, AWS doesn't like stringified JSON in the errorMessage property, so I now just output a simple string like this:

new Exception(name |> sprintf "404: %s not found") |> raise

and using the regex 404:.*, it now works. That means, I somehow have to construct my desired output object using the mappings in the API gateway.

This is quite inconvenient and something easy to trip over...

like image 91
rabejens Avatar answered Oct 20 '22 14:10

rabejens