Reputation: 75
I have a working angular interceptor service which redirects to a log in page on a 401 response being received from a service call
The issue is that the 401 is still shown as a JavaScript error in the Google Chrome console
GET http://domain:port/api/url 401 (Unauthorized) ... angular.js:9658
Is this a feature of chrome, or do I need to handle this in another way?
The contents of my working interceptor service is as follows
var _request = function (config) {
/*insert auth headers*/
}
var _responseError = function (rejection) {
if (rejection.status === 401) {
$location.path('/login');
}
return $q.reject(rejection);
}
var _response = function (response) {
return response || $q.when(response);
}
Thanks in advance for any help
Upvotes: 1
Views: 743
Reputation: 5270
It seems you misunderstand [Angular interceptor]'s mechanism.
For purposes of global error handling, authentication, or any kind of synchronous or asynchronous pre-processing of request or postprocessing of responses, it is desirable to be able to intercept requests before they are handed to the server and responses before they are handed over to the application code that initiated these requests.
Interceptor's purpose is pre-processing responses before they are handed over to application code, which means, it's between the browser and your personal code. As the server responds with 401 errors, the browser will definitely regard it as 401 unauthorized, mark it as red and write logs to console
panel.
Then Angular Interceptor receives http response data from browser and pre-process it as you told it to do. When all these are done, your $http
call would receive the final response object.
So yes, it's a feature of browser. If you think the error message is annoying and you don't want users see it, you can rewrite your response code to others(like 0, 600, anything that doesn't conflict with all HTTP response codes) then handle them in your interceptor. This is a trick.
Upvotes: 1