There doesn't seem to be a direct way to handle errors for individual promises, but you can customize Angular's default error handling behavior by overriding it. This allows you to specify how errors should be handled, especially those that are not caught elsewhere in Angular:
angular.
module('customErrorHandler', []).
factory('$exceptionHandler', ['alertService', function(alertService) {
return function myExceptionHandler(exception, cause) {
alertService.alertError(exception, cause);
};
}]);
If you specifically need to handle errors from a particular function like getFoo()
, you can modify the error object to include identifying information:
function getFoo() {
return $http.get("/not_exists")
.catch(function (error) {
error.errorSource = 'getFoo';
throw error;
});
}
// somewhere else...
angular.
module('customErrorHandler', []).
factory('$exceptionHandler', ['alertService', function(alertService) {
return function myExceptionHandler(exception, cause) {
if(exception.errorSource === 'getFoo') {
alertService.alertError(exception, cause);
} else {
// default error handling
}
};
}]);