I don't believe there is a way to do this for individual promises, but it is possible to override Angular's default error handling which as of Angular1.6 should only handle errors that are not caught elsewhere:
angular.
module('exceptionOverwrite', []).
factory('$exceptionHandler', ['alertService', function(alertService) {
return function myExceptionHandler(exception, cause) {
alertService.alertError(exception, cause);
};
}]);
If you want to add handling to specifically handle errors from getFoo()
, you can have getFoo()
inject some information into the error to make it identifiable:
function getFoo() {
return $http.get("/not_exists")
.catch(function (error) {
error.errorSource = 'getFoo';
throw error;
});
}
// elsewhere...
angular.
module('exceptionOverwrite', []).
factory('$exceptionHandler', ['alertService', function(alertService) {
return function myExceptionHandler(exception, cause) {
if(exception.errorSource = 'getFoo') {
alertService.alertError(exception, cause);
} else {
// default error handling
}
};
}]);