I am trying to figure out the best way to perform error handling in AngularJs. I am making API calls via $resource and I came up with the following code:
emailService.create($scope.emailTemplate).$promise.then(function(data) {
if (data.success) {
$rootScope.showSuccess("Template created.");
$scope.reset();
}
}, function (error) {
if (error.data != null) {
$rootScope.showError(error.data);
} else {
$rootScope.showError();
}
});
$rootScope.showError() and .showSuccess are just basic functions that display a message in a div.
Is there anyway to intercept $resource errors and perform the logic above without having to liter my controller with this code in every call I make?
Thank you!