Handling AWS Lambda Errors with API Gateway

I have a class BadRequest(Exception): pass

lambda in my function.

I would like to raise BadRequest("Invalid request params")

and return an API response with a 400 status code and body { "message": "Invalid request params" }

(or equivalent).

It just does it, but it returns a response with status 200 (oh no!) And body

{
    "errorMessage": "Invalid request params",
    "errorType": "BadRequest",
    "stackTrace": <my application code that the user shouldnt see>
}

      

After searching the web, it seems I have 3 options:

1) chalice

2) Use the integration response and method response to analyze this error in a more efficient response. I would use a regex like [BadRequest].*

and insert the prefix when I throw an exception (not very elegant IMO).

3) Use "Step Functions" to create a view of the state of the API. It seems a little tedious because I will need to learn ASL and I don't know deaf people. -.-

-.- the language of states of the Amazons


Which rabbit hole should I go down and why?

+3


source to share


2 answers


You have to catch the exception in a lambda and throw a custom exception like below.



public class LambdaFunctionHandler implements RequestHandler<String, String> {
  @Override
    public String handleRequest(String input, Context context) {

        Map<String, Object> errorPayload = new HashMap();
        errorPayload.put("errorType", "BadRequest");
        errorPayload.put("httpStatus", 400);
        errorPayload.put("requestId", context.getAwsRequestId());
        errorPayload.put("message", "Invalid request params " + stackstace);
        String message = new ObjectMapper().writeValueAsString(errorPayload);

        throw new RuntimeException(message);
    }
}

And then use Option 2  to map the error code .

Integration response:
Selection pattern: ".*"BadRequest".*"

Method response: 500

Mapping template:

#set ($errorMessageObj = $util.parseJson($input.path('$.errorMessage')))
{
  "type" : "$errorMessageObj.errorType",
  "message" : "$errorMessageObj.message",
  "request-id" : "$errorMessageObj.requestId"
}

      

0


source


This is an ideal use case for AWS stepping functions. You will need to configure the API gateway to call directly the state machine that you create.

Goes ASL for the above state machine:

{
  "Comment": "A state machine that executes my lambda function and catches the bad error.",
  "StartAt": "MyLambda",
  "States": {
    "MyLambda": {
      "Type": "Task",
      "Resource": "arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME",
      "Catch": [
        {
          "ErrorEquals": ["BadError"],
          "Next": "BadErrorFallback"
        }
      ],
      "End": true
    },
    "BadErrorFallback": {
      "Type": "Pass",
      "Result": "Put here whatever is the result that you want to return.",
      "End": true
    }
  }
}

      



What this will do is run your provided lambda functions. If the lambda function throws BadError, then it will output the result of the BadErrorFallback status. Otherwise, it will return all the lambda functions.

Hope this helps!

0


source







All Articles