Skip to content

Instantly share code, notes, and snippets.

@WalterInSH
Created May 25, 2015 07:19
Show Gist options
  • Save WalterInSH/6c32ce1353f7fb940027 to your computer and use it in GitHub Desktop.
Save WalterInSH/6c32ce1353f7fb940027 to your computer and use it in GitHub Desktop.
angular http error handler
/**
* AngularJS v1.3.15
*/
var app = angular.module('fds-punisher', modules, function ($routeProvider, $locationProvider, $httpProvider) {
$httpProvider.interceptors.push(function($q) {
return {
'responseError': function(rejection) {
var status = rejection.status;
if (status == 400) {
warn("提交数据有误:"+rejection.data.message);
}else if(status == 401){
warn("操作失败,因为权限受限")
}else if(status == 403){
warn(response.message)
}else if (status >=500) {
warn("服务暂不可用");
}else{
warn("系统异常");
}
}
};
});
});
/**
* AngularJS v1.2.21
*/
var app = angular.module('fds', modules, function ($routeProvider, $locationProvider, $httpProvider) {
var interceptor = ['$rootScope', '$q', function (scope, $q) {
function success(response) {
return response;
}
function error(response) {
var status = response.status;
if (status == 400) {
warn("提交数据有误:"+response.data.message);
}else if(status == 401){
warn("操作失败,因为权限受限")
}else if(status == 403){
warn(response.data.message)
}else if (status >=500) {
warn("服务暂不可用:" + response.data.message);
}else{
warn("系统异常");
}
}
return function (promise) {
return promise.then(success, error);
}
}];
$httpProvider.responseInterceptors.push(interceptor);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment