Skip to content

Instantly share code, notes, and snippets.

@che5ya
Last active December 25, 2023 15:04
Show Gist options
  • Select an option

  • Save che5ya/7cdf3deadd639f15ab91dd493e8c16e5 to your computer and use it in GitHub Desktop.

Select an option

Save che5ya/7cdf3deadd639f15ab91dd493e8c16e5 to your computer and use it in GitHub Desktop.
Handling HTTP 401 & 50x error codes in catch syntax
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