Last active
December 25, 2023 15:04
-
-
Save che5ya/7cdf3deadd639f15ab91dd493e8c16e5 to your computer and use it in GitHub Desktop.
Handling HTTP 401 & 50x error codes in catch syntax
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
| const HTTP_UNAUTHORIZED = 401; | |
| const HTTP_SERVER_ERRORS = [500, 501, 502, 503, 504, 505, 506, 507, 508, 510, 511]; | |
| function determineError(err) { | |
| const errStatus = err.response.status; | |
| const blameServer = HTTP_SERVER_ERRORS | |
| const unAuthorizedCode = HTTP_UNAUTHORIZED; | |
| let isServerErr = false; | |
| let unAuthorized = false; | |
| if (errStatus === unAuthorizedCode) { | |
| isServerErr = false; | |
| unAuthorized = true; | |
| return { isServerErr, unAuthorized }; | |
| } | |
| blameServer.find(code => { | |
| if (code === errStatus) { | |
| isServerErr = true; | |
| unAuthorized = false; | |
| return { isServerErr, unAuthorized }; | |
| } | |
| }); | |
| } | |
| export default determineError; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment