Reputation: 12203
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.
Can anyone confirm whether $http service should be passing a message to $exceptionHandler in the case of a 500 http status response code?
Upvotes: 1
Views: 825
Reputation: 2865
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.
A 500 error wouldn't be considered an uncaught exception. We use the $httpProvider to intercept the responses and deal with 500 codes on their own. 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:
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);
}
};
}]);
Upvotes: 2