question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add AxiosException class

See original GitHub issue

Is your feature request related to a problem? Please describe. I use typescript. How to verify that I received exactly axios error?

Describe the solution you’d like Add custom error class: AxiosException, which is inherited from the standard Error class

Example

try {
  axios.get('...')
}
catch(err) {
  if (err instanceof AxiosException) {
      console.error(err.response);
  }
}

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:14
  • Comments:20 (2 by maintainers)

github_iconTop GitHub Comments

28reactions
CatsMiaowcommented, Sep 14, 2021

You can apply type guards with the isAxiosError helper function.

https://github.com/axios/axios/blob/f3ca6371caa738ba5308d413433d9f676f2e0138/index.d.ts#L168

https://github.com/axios/axios/blob/f3ca6371caa738ba5308d413433d9f676f2e0138/test/typescript/axios.ts#L369-L372

import axios from 'axios';

if (axios.isAxiosError(error)) {
  // error.response?.data ...
}
15reactions
chinesedfancommented, Feb 28, 2019

@ruscon I think you can use type assertion.

import axios, {AxiosError} from 'axios';

try {
  axios.get('...')
}
catch(err) {
  if ((<any>err).isAxiosError) { // check to make sure type assertion is right
      const e = <AxiosError>error;
      console.error(e.response);
  }
}

Read more comments on GitHub >

github_iconTop Results From Across the Web

Handling Errors | Axios Docs
{ // The request was made and the server responded with a status code // that falls out of the range of 2xx...
Read more >
Handling Errors With Axios - Stack Abuse
In this article, we will see how to handle errors with Axios, as this is very important when making any HTTP calls knowing...
Read more >
axios Error typescript, annotation must be 'any' or 'unknown' if?
In typescript, if you want to catch just a specific type of exception, you have to catch whatever is thrown, check if it...
Read more >
Axios & Error handling like a boss - DEV Community ‍ ‍
It's a awesome way of checking permission, add some header that need to be present, like a token, and preprocess responses, reducing the...
Read more >
How to handle API errors in your web app using axios
The second class of errors is where you don't have a response but there's a request field attached to the error. When does...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found