[SOLVED] Http interceptors problem

I pushed a simple http interceptor in $httpProvider that logs every response (also for resources):

'response': function( response ) {
  console.log( response.config.url + ': ' + response.status );
  return response;
}

When status is 200 all goes well. But when there is a status code 401 the interceptor is ignored and in logs it seems catched by ionic bundle:

error: GET http://MY_URL 401 (Unauthorized) - ionic.bundle.js:19341

After the resource function call the reject function is called so I can see the response status from there, but the interceptor doesn’t log a line.

Any idea?

Errors like 401 should be handled in ‘responseError’ function not in ‘response’.

2 Likes

Thank you, solved now.

Could you post the whole interceptor code? I will need this very soon, and you would save me some googling…

Sure, in module config:

$httpProvider.interceptors.push( function( $q, $injector ) {
	return {
		responseError: function( response ) {
			// console.log( '[ERR] ' + response.config.url + ': ' + response.status );
			if( response.status == 401 || response.status == 403 ) $injector.get( '$state' ).go( 'login' );
			return response;
		},
	};
});