0
votes

I have Python based Lambda which is integrated with API Gateway and try to handle lambda exception below.

{
  "stackTrace": [
    [
      "/var/task/index.py",
      14,
      "handler",
      "raise Exception(err)"
    ]
  ],
  "errorType": "Exception",
  "errorMessage": "device name existed"
}

I've defined HTTP status 400 of Method Response for the exception.

enter image description here

I also defined "Lambda Error Regex" and "Body Mapping Templates".

enter image description here

But the API Gateway still responses HTTP status 200 instead of 400 when my lambda raised an exception.

Anyone has idea what's going wrong. Did I miss something?

1

1 Answers

0
votes

I found the solution by myself.

Here is my Lambda code:

    raise Exception({
        "errorType" : "Exception",
        "httpStatus": 400,
        "message": err
    })

API Gateway will get the error like

{
   "stackTrace": [["/var/task/index.py", 17, "handler", "\"message\": err"]], 
   "errorType": "Exception", 
   "errorMessage": "{'message': 'device not found', 'errorType': 'Exception', 'httpStatus': 400}"
}

The reason why my lambda error regex didn't work is that the error message is within single quotes instead of double quotes.

After I changed the lambda error regex like below, everything works fine

.*'errorType': 'Exception'.*