Skip to content
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

responseError not raised when my http get 401 UnAuthorized error #16

Closed
wyshmily opened this issue Nov 16, 2017 · 7 comments
Closed

responseError not raised when my http get 401 UnAuthorized error #16

wyshmily opened this issue Nov 16, 2017 · 7 comments
Labels

Comments

@wyshmily
Copy link

Why?

@mlegenhausen
Copy link
Owner

mlegenhausen commented Nov 16, 2017

This is cause the native fetch library makes no assumptions about the response code if it is an error or a wanted behaviour. You can convert your response to an error and handle it afterwards. Example

fetchIntercept.register({
  response: function (response) {
    if (response.code >= 400) throw new Error('Request error');
    return response;
  }
});

fetchIntercept.register({
  responseError: function (error) {
    // Handle your error thrown from the previous interceptor here
  }
});

@wyshmily
Copy link
Author

Thanks

@VasiliyPolyakov
Copy link

VasiliyPolyakov commented Jun 14, 2018

@mlegenhausen Hello! Can you help me? I have the same issue. I did everything that you wrote, but unfortunately, I could not handle the error in responseError.
fetchIntercept.register({
request: function(url, config) {
// Modify the url or config here
return [url, config];
},

requestError: function(error) {
// Called when an error occurred during another 'request' interceptor call
return Promise.reject(error);
},

response: function(response) {
if (response.status >= 400) throw new Error('Request error');
return response;
},

responseError: function(error) {
alert(error);
return Promise.reject(error);
}
});

@mlegenhausen
Copy link
Owner

You need to call fetchIntercept twice this is important cause these calls build up a promise chain internally. This means you need to split up you register in two calls like in my example.

@VasiliyPolyakov
Copy link

Hello. Thank you for the answer. I did everything like you, but unfortunately, my component (showNotification) was not called in responseError for some reasons. I can't understand why. Maybe I am doing something wrong

my code:
fetchIntercept.register({
response: function(response) {
if (response.status >= 401) throw new Error('Request error');
return response;
}
});

fetchIntercept.register({
responseError: function(error) {
console.log('error', error)
showNotification(error);
}
});

@mlegenhausen
Copy link
Owner

Try out to regsiter the responseError before the response listener. Internally I reverse the order of the interceptors to conform with the angular behavior.

@VasiliyPolyakov
Copy link

@mlegenhausen awesome!!! It works. Thank you very much for quick answer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants