Last active
August 29, 2015 13:57
-
-
Save gruppjo/9385395 to your computer and use it in GitHub Desktop.
angular interceptors example setup (with $provide in config)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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