Last active
August 29, 2015 13:58
-
-
Save jvans1/10119587 to your computer and use it in GitHub Desktop.
errors http interceptor
This file contains hidden or 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
angular.module('myapp').config([ '$httpProvider', function ($httpProvider) { | |
$httpProvider.interceptors.push('globalErrorInterceptor'); | |
}]); | |
angular.module('myapp').factory('globalErrorInterceptor', ['$q', '$rootScope', '$cookieStore', '$injector', '$location', 'PathService', 'AlertService', | |
function ($q, $rootScope, $cookieStore, $injector, $location, Path, Alert) { | |
$rootScope.showSpinner = false; | |
$rootScope.http = null; | |
return { | |
'response': function (response) { | |
return response || $q.when(response) | |
}, | |
'responseError': function (rejection) { | |
switch (rejection.status) { | |
case 401: | |
$location.path("/login") | |
$cookieStore.put('currentUser', undefined) | |
Alert.add("warning", 'You must be logged in to perform that action', 1400 ) | |
break; | |
case 404: | |
Alert.add("danger", 'The requested resource couldn\'t be located', 1400 ) | |
break; | |
case 422: | |
var validationErrors = []; | |
angular.forEach(rejection.data.errors, function(value, key){ | |
validationErrors.push(key + " " + value.join(', ')) | |
}); | |
console.log(validationErrors.join(', ') ) | |
Alert.add('danger', validationErrors.join(', ') ) | |
break; | |
case 500: | |
Alert.add("danger",'Server internal error: ' + rejection.data) | |
break; | |
} | |
return $q.reject(rejection); | |
} | |
} | |
} | |
]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment