Reputation: 258
I am trying to send a API Gateway Response code of 401 if the user is missing required fields in header. The response from my lambda is as follows:
{
"status": "Unauthorized user",
"body": ["username"]
}
In the AWS API Gateway -> myAPI -> resource -> PUT operation -> Integration Response, I have 2 entries.
1. For all success with default RegEx Pattern (blank) --> Method response status of 201
2. For regex matching, if I use above JSON and below regex using regex or online regex101.com, there is a full match.
.*\"Unauthorized user\".*
However, the same fails during Test of API with below message
<date_time in UTC> : Endpoint response body before transformations: {"status": "Unauthorized user", "body": ["username"] }
<date_time in UTC> : Execution failed due to configuration error: No match for output mapping and no default output mapping configured. Endpoint Response Status Code: 200
<date_time in UTC> : Method completed with status: 500
Has anybody faced this? I end up getting a 201 if I have 201 with blank regex... irrespective of Unauthorized or internal error or bad requests.
Upvotes: 2
Views: 2073
Reputation: 1363
This is the image from your question:
Notice how the header of the first column says Lambda Error Regex
? That means that API Gateway will only try to match that regex with an error returned by the Lambda. But how does the API Gateway know whether the Lambda returned a genuine response or an error? It knows this based on whether the Lambda return
ed a response or threw
an exception.
Your regex isn't matching because you returned the response instead of throwing it as an exception!
Check this out:
When my Node.js Lambda does this:
exports.handler = async (event) => {
const response = {
"status": "Unauthorized user",
"body": ["username"]
}
return response
}
My API gateway does this:
But when the same Lambda does this:
exports.handler = async (event) => {
const error = {
"status": "Unauthorized user",
"body": ["username"]
}
throw JSON.stringify(error)
}
The API Gateway does this:
Upvotes: 3