Last active
November 16, 2020 19:06
-
-
Save bearkfear/e9221d13398788e7b3fedfcbaaf9c2d4 to your computer and use it in GitHub Desktop.
How handle errors in Axios with TypeScript
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
function errorHandler(error: AxiosError) { | |
type ErrorBody = { | |
message?: string; | |
error?: string; | |
} | |
if (error.response) { | |
// error in server with a response | |
const data = (<ErrorBody>error.response.data) | |
const body = `${data.error}: ${data.message}`; | |
alert(body); | |
} else if (error.request) { | |
// Error in request | |
alert("Por favor, verifique sua conexão com o servidor!"); | |
} else { | |
// error in code | |
console.log("DEVELOPER DEBUG: error in some implementation") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment