Created
July 29, 2021 09:41
-
-
Save leegilmorecode/fe3ae00899d9ecd3b75cf357cc025aa9 to your computer and use it in GitHub Desktop.
Very basic example of a generic error handler
This file contains hidden or 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
| // basic error handler which will not expose any stack traces/secrets | |
| // and will return the correct responses when invalid params from validator | |
| export const errorHandler = (error) => { | |
| let body = "An error has occurred"; | |
| let statusCode = 500; | |
| const errorType = error && error.errorType ? error.errorType : 2; | |
| switch (errorType) { | |
| case 1: | |
| body = "Forbidden"; | |
| statusCode = 403; | |
| break; | |
| case 2: | |
| case 3: | |
| break; | |
| case 4: | |
| case 5: | |
| body = { message: error.userMessage }; | |
| statusCode = 400; | |
| break; | |
| case 6: | |
| body = "Not Found"; | |
| statusCode = 404; | |
| break; | |
| case 7: | |
| body = "Unauthorised"; | |
| statusCode = 401; | |
| break; | |
| default: | |
| break; | |
| } | |
| return { | |
| statusCode, | |
| headers: { | |
| "Content-Type": "application/json", | |
| }, | |
| body: JSON.stringify(body, null, 2), | |
| }; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment