Handle error codes (401, 403) with Restangular
angularjs, restangular
Solution
401 and any error code will hit the errorInterceptor, not the responseInterceptor. responseInterceptor is only called with successful responses!
Problem
I am building an app using Restangular. I would implement in one place handling of errors like 401, 403, 500 etc in a response interceptor. This is what I wrote: ``` RestangularProvider.setResponseExtractor(function(response, operation, what, url) { switch(response.meta.code) { case 200: // Handle 200 break; case 401: // Redirect to login break; /* ... */ default: break; } } ``` What actually happens it's that the 200 is correctly seen but this method is not hit at all for the 401. The JSON I get from server side it is formatted like this for successful responses: ``` {"meta":{"apiVersion":"0.1","code":200,"errors":null},"response":{"response":"value"}} ``` and ``` {"meta":{"apiVersion":"0.1","code":401,"errors":[{"code":401,"message":"Unauthorized"}]},"response":null} ``` for errors. The response header contains the `Status Code:401 Unauthorized` as well. What am I doing wrong?