Last active
May 18, 2020 10:54
-
-
Save ThomasAribart/58498d3e87824f69e311205fd2912bd0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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