Skip to content

Instantly share code, notes, and snippets.

@ThomasAribart
Last active May 18, 2020 10:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThomasAribart/58498d3e87824f69e311205fd2912bd0 to your computer and use it in GitHub Desktop.
Save ThomasAribart/58498d3e87824f69e311205fd2912bd0 to your computer and use it in GitHub Desktop.
const createProxyHttpResponse = ({ statusCode, key, message }) => ({
statusCode,
body: JSON.stringify({ key, message }),
headers: { 'Content-Type': 'application/json' },
});
const formatErrorForApiGateway = async ({ error, callback }) =>
callback(
null,
createProxyHttpResponse(
error instanceof CustomError
? error
// Catch non-custom errors
: new InternalServerError({ message: error.message })
)
);
const businessLogic = async () => {
...
throw new BadRequestError({
key: "INVALID_TOMATO_COLOR",
message: "Tomatoes should be red"
})
...
};
const myHandler = middy(businessLogic)
.onError(formatErrorForApiGateway);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment