Skip to content

Instantly share code, notes, and snippets.

@bearkfear
Last active November 16, 2020 19:06
Show Gist options
  • Save bearkfear/e9221d13398788e7b3fedfcbaaf9c2d4 to your computer and use it in GitHub Desktop.
Save bearkfear/e9221d13398788e7b3fedfcbaaf9c2d4 to your computer and use it in GitHub Desktop.
How handle errors in Axios with TypeScript
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