API Gateway HttpApi with Lambda - Error handling/response.

I suppose this could be a broader question than being specific to API Gateway, but then there’s also the whole expected Lambda response thing. I want to make sure my API behaves similar to other API’s out there, and while I know there’s no real “standard”, there are some general guide lines people follow I feel.

I think my biggest question is when I respond with an error from my Lambda function, say a 400 Bad Request when a user doesn’t provide all expected parameters, do I put the error message in the “body” of the response? E.g. would my return statement look something like:

return {
“statusCode”: 400,
“body”: json.dumps({“message”: “MissingParameterException: Please provide all required params.”})
}

Or would the error message go in a completely separate field, like “errorMessage” or something like that? I tried that, but then I get an internal server error from API Gateway. I also tried passing it in “message” instead of “body”, but then my curl doesn’t do anything when I test it. I have to run with -v to see that I got a 400, and I can’t actually see the message.

I read through the AWS docs, but they are really basic. It references a header you can set to return the errorCode, but then there’s no reference to all available headers like that.

At the end of the day, all I want to know is what is good practice with HttpApi’s with Lambda backends. I’d like to use as much of the niceties AWS provides with this integration. I.e. as little need for mapping crap as possible, but at the same time maintaining best practices.

Any guidance would be greatly appreciated.

I prefer application errors to return 200 even if validation fails. Then there is a result column and message. Result has failure . Message can have error message. And then there is data if the result is success.

Result data and message go in the body

Not sure which language/framework you are using to create a function, but i am sure you can find the equivalent of the below code:
I did this in one of my sample projects with Node and Express and it worked fine.

res
.status(404)
.json({ error: ‘Could not find user"’ });
Where res is the standard res in Node Js + Express application.

The practices on HTTP should ideally be same as that of REST; it’s not a standard as such like REST, it’s a way AWS allows you to use the trimmed down version of REST to save cost and hide the complexities.

in go.

func handler(w http.ResponseWriter, r *http.Request){
  w.WriteHeader(http.StatusBadRequest)
  w.Header().Add("Content-Type", "application/json")
  fmt.Fprintf(w, "{\"message\":\"MissingParameterException -----\"}")
}