Trying to go serverless with apigateway but having an issue getting Lambda endpoints to return any HTTP Status Code other than 200. I can of course return JSON with status set to whatever I want, but I need to set the HTTP Status to match.
exports.handler = async (event, context, callback) => {
return new Promise((resolve,reject) => {
findData().then((data) => {
// found data
resolve({
status: 200,
body: data
});
},(err) => {
// didn't find anything
reject({
status: 404,
error: {
message: 'not found'
}
});
});
});
};
I cannot figure out how to get lambda and apigateway to return HTTP Status Code 404 in the error case. I have also tried the documented
{
statusCode: 404,
headers: {},
body: err
}
But that does not work either. I have also change the error side of the promise to call resolve (instead of reject)...it of course works but the HTTP Status code is always 200 no matter what I try.
I am open to using Response Mappings, but that isn't so straight-forward and I cannot find any examples.....
Thanks for your time.
Edited by: rjsoph on Feb 10, 2019 1:59 PM
Edited by: rjsoph on Feb 10, 2019 2:01 PM