Angular JS 1.x http interceptors
Angular js 1.x provides the $http service to allow the client side to communicate with the backed using HTTP requests. For most complex usages it is often necessary to do some processing with the requests/responses before they reach our controllers. For example, one might be looking to implement a global error handler that displays a fixed error page for any server side errors.
Interceptors are basically an array of services available to the $httpProvider service that pass the HTTP requests and/or responses through these services to perform necessary operations such as logging, error handling etc. before the requests are forwarded to the server or the responses are made available to the controllers.
There are four kinds of interceptors available:
- request: interceptors get called with a http config object. The function is free to modify the config object or create a new one. The function needs to return the config object directly, or a promise containing the config or a new config object.
- requestError: interceptor gets called when a previous interceptor threw an error or resolved with a rejection.
- response: interceptors get called with http response object. The function is free to modify the response object or create a new one. The function needs to return the response object directly, or as a promise containing the response or a new response object.
- responseError: interceptor gets called when a previous interceptor threw an error or resolved with a rejection.