How to use react-query with an axios interceptor? #2874
-
Hello all, I want to have an Axios interceptor so I can set the proper error message depending on what has happened, something like this:
Now, my React Query is like this:
And my fetchBlabla is:
However, I keep getting uncaught exceptions or exceptions saying " response is undefined ". How is the best way to handle this? I have tried adding try/catch on fetchBlabla but to make it work properly and I have to rethrow the error within the catch, making a red exception appear on the console. I added the rethrow because on the docs say fetchBlabla should throw an error in order to React-Query pick it, however, I am not really a fan of throwing error and make the console red. Also, why to rethrow again an error that already exists? Does this make sense? Thank you in advance and regards |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
I tried to answer this on stackoverflow: https://stackoverflow.com/questions/69838642/axios-intercept-error-promise-or-throw-error For anything more, I think I'd need a codesandbox reproduction that shows your error please. |
Beta Was this translation helpful? Give feedback.
-
@TkDodo How can we use error boundary to catch Network error and show a network error fallback ui and if internal logical error in react qith reacr query. |
Beta Was this translation helpful? Give feedback.
-
@javierguzman I think the issue might be that you DON'T return that rejected promise. This effectively breaks the promise chain - Hello "Uncaught rejection" :) The another issue might be that your error handler is not async function as you have just Try:
So I would say you either need to return rejected promise or make your handle async. |
Beta Was this translation helpful? Give feedback.
I tried to answer this on stackoverflow: https://stackoverflow.com/questions/69838642/axios-intercept-error-promise-or-throw-error
For anything more, I think I'd need a codesandbox reproduction that shows your error please.