Skip to content

Instantly share code, notes, and snippets.

@gruppjo
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gruppjo/9385395 to your computer and use it in GitHub Desktop.
Save gruppjo/9385395 to your computer and use it in GitHub Desktop.
angular interceptors example setup (with $provide in config)
'use strict';
angular.module('tradecore')
.constant('Config', {
baseUrl: 'http://62.26.167.30/tca'
})
.config(function ($provide, $httpProvider) {
$provide.factory('errorHandlerInterceptor', function ($q) {
// be careful when injecting services here
// circular dependencies will occur when injecting services that use $http
return {
// optional method
request: function (config) {
// do something on success
return config || $q.when(config);
},
// optional method
requestError: function (rejection) {
// do something on error
if (canRecover(rejection)) {
return responseOrNewPromise;
}
return $q.reject(rejection);
},
// optional method
response: function (response) {
// only handle remote XHR requests
if (response.config.url.search(/\/\//) !== -1) {
// do something on success
}
return response;
},
return response || $q.when(response);
},
// optional method
responseError: function (rejection) {
// do something on error
if (canRecover(rejection)) {
return responseOrNewPromise;
}
return $q.reject(rejection);
}
};
});
$httpProvider.interceptors.push('errorHandlerInterceptor');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment