Custom Error and Status from Lambda and API Gateway

Recently I was working on developing the API and faced the issue while returning the custom HTTP response to the API end-point.

On the examples provided by AWS and other blog-post, they all mentioned either old methods or using Node.js code for the demo.

So, I thought of putting it all together and write an article about it. After all, be the change you want to see in the world and I also don't want to waste too much of your time looking for the solution.

So, here is what you need to know before jumping on the solution.

When you are working with Lambda and API Gateway — even though the Lambda for some of the input not successful — it returns the status code as 200.

Let’s try something simpler:-

def lambda_handler(event, context):
raise Exception(‘Something bad happened!’)

When you call this code after integrating with AWS API Gateway — it returns the Status as 200:OK

Status code returned as 200 even though we have raised an error

Now I don't like this kind of response because even though we have encountered the error, we are receiving 200 OK status (because Lambda execution was successful).

So, to resolve this kind of error and have your own HTTP error status codes — we need to perform the mapping at the API gateway end.

Let’s take the almost similar code.

def lambda_handler(event, context):
raise Exception('400:Something bad happened!')

As you can see, we have now added the code in front. Just mentioning in the Exception statement will not have any effect on the result until we do the mapping using lambda Regex.

How to do that?

Go to the Method response in the API gateway.

Method Response will help us to define various HTTP code

Declare the error code of your choice.

200 is the default one — I have added 404 HTTP Status code

Go to Integration Response where you will define the Lambda regex and map the error.

Mention the regex — For our case, as we are passing the code “400:Something bad happened!” in the Exception message, we are asking the API gateway to look for 400:Something into error response!

Mention the Lambda Error Regex

And that’s it !! Test the response now!

Now we are receiving the response code as 400 (Remember! You can map it to anyone!!

So the current response looks like as below!

{   "errorMessage": "Something 400  bad happened!",   "errorType": "Exception",   "stackTrace": [   " File \"/var/task/lambda_function.py\", line 5, in lambda_handler\n    raise Exception('Something 400  bad happened!')\n"]}

Now I don't like the error type as “ Exception”.I want to change that!

class My_402_Exception(Exception):
pass

def lambda_handler(event, context):
raise My_402_Exception('Error 402 :A very specific bad 402 thing happened')

Let's do the mapping for code 402.

402 code mapping

Check out the response now

Status as 402

So now we have the erroType in response as My_402_Exception

Of Course, this error status code can be anything! So choose your status code wisely and try not to use the famous error codes too loosely.

Now, I don't want stack trace in my response, how do I remove it?

import sysclass My_402_Exception(Exception):
pass

def lambda_handler(event, context):
sys.tracebacklimit = 0
raise My_402_Exception('Error 402 :A very specific bad 402 thing happened')

Let’s see the impact of the code once we deploy the code.

Stacktrace is no longer present in the response!!

So, I guess this one is a bit easy and you can explore with different error codes of your liking !!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store