Angular writes an error to the console before raising prom.catch

It seems to me that I'm a little crazy, but Angular seems to throw an error for a promise, even if I have a catch . It throws an error to the console, and then launches catch .

Here is a super-simple violin

Security Code:

 $q.when(1) .then(function() { throw new Error("test"); }).catch(function(error) { console.log('error caught', error); }); 

Received Console

enter image description here (dirty liar!)

Here is a script showing what I expect to see : catch raised and no other error is logged to the console, Could I configure something, or did Angular implement the broken promise specification?

+6
source share
1 answer

angular logs all errors for the console by default.

angular also provides the ability to override this behavior. $exceptionHandler is a global service that gets the ability to handle any exceptions ($ http, errors during $ digest, etc.).

If you add this piece of code:

 myApp.factory('$exceptionHandler', function() { return function(exception, cause) { exception.message += ' (caused by "' + cause + '")'; //throw exception; }; }); 

Then all errors simply stopped further logging. At least catch () handlers. Updated fiddle: http://jsfiddle.net/5jjx5rn3/

UPDATE:

As pointed out in dnc253 comments, there is a better way if you intend to actually override the angularjs service. Even without being the center of attention of this issue, it is better to know that simply by declaring a service with the same name, in any module the service is completely replaced (last winnings). If you want to add functionality around the original service, decorator is the right choice.

+6
source

Source: https://habr.com/ru/post/982909/


All Articles