[英]Does Angular $http Service Call into the $exceptionHandler on Http Error Responses
I was under the impression that http errors returned from requests inside of the angular $http service would be passed through the $exceptionHandler, but I am seeing some behavior that makes me think otherwise. 我的印象是,有角度的$ http服务内部的请求返回的http错误将通过$ exceptionHandler传递,但是我看到某些行为使我不以为然。
Can anyone confirm whether $http service should be passing a message to $exceptionHandler in the case of a 500 http status response code? 任何人都可以确认$ http服务是否应在500 http状态响应代码的情况下将消息传递给$ exceptionHandler?
To my knowledge, no they don't pass through the exception handler. 据我所知,不,它们不会通过异常处理程序。 The angular documentation states: 角度文档指出:
Any uncaught exception in angular expressions is delegated to this service. 角度表达式中任何未捕获的异常都委托给此服务。 The default implementation simply delegates to $log.error which logs it into the browser console. 默认实现只是将$ log.error委托给浏览器控制台。
A 500 error wouldn't be considered an uncaught exception. 500错误不会被视为未捕获的异常。 We use the $httpProvider to intercept the responses and deal with 500 codes on their own. 我们使用$ httpProvider拦截响应并自己处理500个代码。 We made a service to handle this functionality. 我们提供了一项服务来处理此功能。
Our app config looks like this: 我们的应用程序配置如下所示:
appModule.config(['$routeProvider', '$locationProvider', '$httpProvider', '$provide',
function ($routeProvider, $locationProvider, $httpProvider, $provide) {
// Http interceptor to handle session timeouts and basic errors
$httpProvider.responseInterceptors.push(['httpHandlersSrv', function (httpHandlersSrv) {
return function (promise) { return promise.then(httpHandlersSrv.success, httpHandlersSrv.error); };
}]);
routeProvider = $routeProvider;
$locationProvider.html5Mode(true);
}
]);
This is what our $httpHandlersSrv looks like where we handle the 500 code errors: 这就是我们的$ httpHandlersSrv看起来像处理500个代码错误的地方:
angular.module('appModule').factory('httpHandlersSrv', ['$q', '$location', '$rootScope', 'toaster', '$window', function ($q, $location, $rootScope, toaster, $window) {
return {
success: function (response) {
return response;
},
error: function (response) {
switch (response.status) {
case 0:
//Do something when we don't get a response back
break;
case 401:
//Do something when we get an authorization error
break;
case 400:
//Do something for other errors
break;
case 500:
//Do something when we get a server error
break;
default:
//Do something with other error codes
break;
}
return $q.reject(response);
}
};
}]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.