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.

Is the breaking change in the type definition of AxiosError as intended?

See original GitHub issue

Describe the bug

The definition of AxiosError had been updated at v0.27.0.

https://github.com/axios/axios/pull/3645/files#diff-7aa4473ede4abd9ec099e87fec67fd57afafaf39e05d493ab4533acc38547eb8L124-R124

It seems compatible with the previous code, but this changeset is not based on #4186.

In v0.26.1, AxiosError defined as <T = any, D = any> extends Error.

https://github.com/axios/axios/blob/v0.26.1/index.d.ts#L139

As a result, AxiosError has a different type definition than AxiosPromise or AxiosResponse. The former <T=unknown>, the latter <T=any>. I think these inconsistent definitions are undesirable.

It seems to me that this breaking change is unintentional, isn’t it?

To Reproduce

if (axios.isAxiosError(e)) {
  console.log(e.response.data.message)
}

Got an error on e.response.data.

Object is of type ‘unknown’.ts(2571)

Expected behavior

No errors occur.

Environment

  • Axios Version: 0.27.2

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:19
  • Comments:11

github_iconTop GitHub Comments

8reactions
dimbslmhcommented, Jul 7, 2022

The following seems to work with v0.27:

if (e instanceof AxiosError) {
  console.log(e.response.data.message)
}
6reactions
martinfoakescommented, May 30, 2022

If this is intentional behaviour then what is the preferred way of handling these error responses? Currently I have an app that frequently accesses the response data/messages for error logging and error message displaying, like so:

...
  } catch (e) {
    if (axios.isAxiosError(e)) {
      datadogLogs.logger.error(e.response?.data);
      message.error(e.response?.data?.message);
    }
  }
Read more comments on GitHub >

github_iconTop Results From Across the Web

I Keep getting, 'Object is of type 'unknown'' error - Stack Overflow
But it complains about the 'data' variable, saying 'Object is of type 'unknown'. After doing some research they said I have to use...
Read more >
axioserror object - You.com | The search engine you control.
You're defining the paypal url as '${PAYPAL_API}/v2/checkout/orders' ... axios/axiosIs the breaking change in the type definition of AxiosError as intended?
Read more >
Release notes - n8n Documentation
Release date: 2022-10-14. Breaking changes. Please note that this version contains breaking changes to the Merge node. You can read more about them...
Read more >
nekoya on Twitter: "Axiosのv0.27、恐らく予期しない型のbreaking ...
Is the breaking change in the type definition of AxiosError as intended? · Issue #4716 · axios/axios. Describe the bug The definition of...
Read more >
TypeScript errors and how to fix them
Set the “target” property in your “tsconfig.json” file to “es5” or higher: ... You have to separate the argument list from the return...
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