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
Interceptor's unable to catch Network failures #651
Comments
Response interceptors intercept any kind of error, including network errors. You need to register your error handling function as the second parameter of |
Hi, I have same problems. axios.interceptors.response.use((response) => {
if (response.data.error) {
return Promise.reject(response);
}
return response;
}, (error) => (
console.log(error); // result -> Error: Network Error(…)
)); How to get the error ? |
Helped for me #383 |
@rubennorte Was planning on a routing every api in my RN app from a single entry point (Controller). At this point, I can handle or update the Requests and Responses, add default params, and importantly - handle errors. I want to show error messages, alerts, dialogs etc from a single place, and prevent exception errors to crash the app |
I think what you're trying to achieve is a way to stop promise propagation under certain conditions. Something like: axios(...).then(handleResponse, handleSpecificErrors); So when this request fails due to a network error, neither This is hard to achieve with promises as they're now (some cancellable promises proposals intend to solve things like this), but something you can do is to return a never resolved promise in your error response interceptor. axios.interceptors.response.use(null, (error) => {
if (!isGenericError(error)) {
return Promise.reject(error);
}
handleGenericError(error);
return new Promise(() => {});
}); I don't like this solution very much as it may be dangerous in certain conditions, but is the only thing you can do to achieve that. You can also manage the errors sharing some logic in the rejection handlers of each call. |
Goal was to keep single point api handling across my React Native app, and manage any errors, messages or common responses from a single place while returning the data if successful.
Tried to use interceptors for this purpose but they couldn't catch errors as mentioned in the read me that they're executed before using .then() or .catch().
Even tried the basic try {} catch()=>{} around interceptors, in vein.
What could be the best way around to achieve this?
The text was updated successfully, but these errors were encountered: