AWS SAM/Api Gateway is swallowing my errors!?

AWS SAM is a great tool that has made my life so much easier when building Lambda functions that are accessible through API Gateway.

The one thing that has caught me out a couple of times is I don't get errors returned through API gateway when using the Node JS starter from $ sam init

As I'm testing I find a bug, as hard as I try I just cant manage to work out that "Just don't write bugs" coding strategy.  When I hit the bug I'm expecting to see the error as the response in Postman because of this block:

} catch (err) {
console.log(err);
return err;
}

What I get though is a 504 response with no body.

While this is part of the starter if you want to return the value through API gateway when an unexpected error occurs you need to have the error wrapped in the same format as the body in the starter.  Like this:

} catch (err) {
console.log(err);
return {
statusCode: 500,
body: JSON.stringify({
err: err
})
};
}

With that in place I get a 500 response in Postman with error details as the body. 


This is as of January 23 2019, running SAM CLI, version 0.10.0.
$ sam --version
SAM CLI, version 0.10.0


Comments

Popular posts from this blog

Solving `Empty reply from server` in DotNet Core

Testing functions that use local storage with Jest

Building a verify JWT function in TypeScript