-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Promise.catch is triggered on request unrelated error #400
Comments
This is how promises work. For example, the code below will print 'caught an error' even if the const promise = axios.get('http://foobar.com/');
promise
.then(() => {
throw new Error('boom!');
})
.catch(err => {
console.log('caught an error', err);
}); The reason why the error handler is called is that Hope this helps! |
Thanks for the explanation, though I still don't really get it. I understand your example, where an error is raised by code inside the callback, but I don't see the connection in my case, where the error happens in another part of application. Can you recommend some approach how to easily distinguish between http request error and some other error? I don't see comparing strings as effective and error-prone way to achieve that. I expect the catch clause handle http request errors only, while all other application errors should be handled in places they can eventually occur. |
The function passed to const promise = axios.get('http://foobar.com/');
promise
.then(() => {
// This is what happens when the `Promise` returned by axios is resolved:
// - dispatch action
// - update state
// - notify React components
// - render
})
.catch(err => {
// If an error is thrown in `render`, the `Promise` returned by `then` will be rejected
// and this error handler will be executed
}); |
Now it makes perfect sense. |
This feels somewhat inelegant, but I found a workaround by wrapping const promise = axios.get('http://example.com/');
promise
.then((response) => {
// Do stuff…
setTimeout(() => {
// Now this dispatch is not connected to the request promise…
dispatch(successAction(response));
});
})
.catch((error) => {
// Now this only catches errors from the request, or other code outside the setTimeout call
}); |
The best way to handle this is to utilize the second argument to
|
When you make axios request and error occurs anywhere in the code during the processing of request, catch error handler is triggered even when the error has nothing to do with the request and the request is successfully processed.
Not sure if it's related to specific libraries, but in my opinion the behaviour is not correct.
Involved libraries are:
Flow is the following:
This makes my application report incorrect error saying that there was problem with the request, but in fact the request was completed and the error was raised after that.
I discovered it in project where I write both client and server so I'm sure that there is no error in the request.
I'm quite new to JavaScript so please explain if I'm wrong, but I believe this is not desired behaviour of error handler.
The text was updated successfully, but these errors were encountered: