4

I'm building an AWS API mail service that basically checks the body of the request before sending a mail with data from that body. It checks if every required parameter is there and does some basic checks if the data conforms to what we require.

The problem is, that even if the Lambda function throws an error (which I verified using the Lambda Test interface), the API Gateway returns a 200 response code with the error object as body

That means that I get a log like this:

Tue Apr 11 14:23:43 UTC 2017 : Method response body after transformations: 
{"errorMessage":"\"[BadRequest] Missing email\""}
Tue Apr 11 14:23:43 UTC 2017 : Method response headers: {X-Amzn-Trace-Id=Root=************, Content-Type=application/json}
Tue Apr 11 14:23:43 UTC 2017 : Successfully completed execution
Tue Apr 11 14:23:43 UTC 2017 : Method completed with status: 200

Because of the last part, I believe API Gateway is returning 200.

I did a couple of things to set up error handling:

  • I added a second Method response for the error code: enter image description here
  • I added a second Integration response for the error code: enter image description here

At this point I'm not sure why it still fails to return a correct response. I checked various posts about this (including: Is there a way to change the http status codes returned by Amazon API Gateway? and How to return error collection/object from AWS Lambda function and map to AWS API Gateway response code) and read the documentation.

I also tried the 'Lambda Proxy Way', but that didn't yield any results (the lambda didn't perform correctly at all that way'.

Does anyone see what I am missing here?

Community
  • 1
  • 1
Lodybo
  • 467
  • 5
  • 13

1 Answers1

2

I see a couple of things that could be causing problems.

Your error message is quoted: "\"[BadRequest] Missing email\"", so the ^[BadRequest] regex won't match the error string. In a simple test I ran, I had to escape the [] (i.e. \[\]) since square brackets are reserved for character classes.

Without changing your errorMessage formatting, a pattern like this should work: ^"\[BadRequest\].*

Mark Mucha
  • 1,560
  • 2
  • 12
  • 18
  • 1
    Yes, that's it. The tutorial I based this on didn't escape the brackets. Thanks man, that saved the day! – Lodybo Apr 12 '17 at 07:31