1

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?

1 Answer 1

2

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);
    }
};
}]);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for pointing out that 500 errors wouldn't be considered exceptions. This was the key for my issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.