I have been using angular's $http service for a project. Before finishing the project, I noticed that the .success
and .error
callbacks have been deprecated.
So now we have .then
and .catch
clauses, fine.
I have a service, that wraps the calls to certain API. Inside that service I wanted to "install" a default error handler in case of a unauthorized request. I used to do it this way:
post = function(encodedData ,headers ,url ) {
headers.Accept = '*/*';
this.setAutentication();
return $http({
'url': url,
'method': 'POST',
'data': encodedData,
'headers': headers
}).error( this.callUnauthorizedHandler.bind(this) );
};
As you can see, I return the $http promise with an error handler already attached. Since the .error
callback returns the original promise, everything worked like a charm. Now I have to first save the promise to a variable, attach the error handler using catch and then return the promise.
Not a big problem, but I feel like there is no way to attach several "error" handlers to a single promise, so if I add another catch
clause, I think I will be overwriting the already installed one. Is this correct? What is the correct way of managing this? All what I have read about promises does not specifies if this idea is valid or absolutely stupid.
EDIT:
I have found this answer: https://stackoverflow.com/a/22415684/1734815 Seems that i have to throw an error from my "default" error handler if it is now able to handle the error by itself, but I'm not sure about this.