I am new to AWS API Gateway, I use it with AWS Lambda (Java).
Apparently the only way to change the response status code (to 4xx, 5xx, etc.) is to return a String that must match the Lambda error regex in the Integration Response section of the resource, or to throw an exception which also should contain a message matching the regex, is that right?
But then if I can only return a String, how do I provide more details for the response entity?
Here is what my RESTful APIs use to return along with an appropriate error status code:
{
"code": "123",
"message": "Invalid email address",
"path": "/email"
}
The code property is useful for debugging or internationalization (i18n), the path is the field to correct.
Sometimes I wrap it in an "error" JSON object, sometimes I return a list of validation errors instead of only 1 error, etc. I customize it according to the project specs.
How can I produce such response entity with API Gateway?
Thanks.
EDIT: thanks to kennbrodhagen's advice, here is what I ended up doing:
1. I stringify my error object (using the Jackson ObjectMapper
)
2. I throw a RuntimeException
with the stringified error as the message
In the Integration Response:
3. I use some property of my error for the Lambda error regex
4. I mapped this template
{ "error": $input.path('$.errorMessage') }
Then the API response (if error occurs) becomes
{ "error": {"code": "123", ... }}
Very basic for now but better than just getting a String in the response.