Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a code with AngularJS:

service.doSomething()
  .then(function(result) {
      //do something with the result
  });

In AngularJS 1.5.9 when I have error in the .then() section like:

service.doSomething()
  .then(function(result) {
      var x = null;
      var y = x.y;
      //do something with the result
  });

I'm getting clear error message:

TypeError: Cannot read property 'y' of null

But in version 1.6 with the same code I'm getting a different error:

Possibly unhandled rejection: {} undefined

I know that this is related to this change, and the single solution is quite simple by adding .catch() block:

service.doSomething()
  .then(function(result) {
      var x = null;
      var y = x.y;
      //do something with the result
  })
  .catch(console.error);

Now I again have what I want:

TypeError: Cannot read property 'y' of null

But how to obtain the same result (more detailed error) for entire application without adding .catch() block in every single place?

I tested the suggested solution to disable this by adding:

$qProvider.errorOnUnhandledRejections(false);

But with this the situation is even worse - I do not have ANYTHING in the console! The error is swallowed somewhere and not logged at all. I'm not sure is it a problem with AngularJS 1.6 or with my configuration.

Do you have any ideas how to "restore" logging behavior from version 1.5.9?

EDIT:

Adding custom error handler:

.factory('$exceptionHandler', function($log) {
  return function(exception, cause) {
    $log.warn(exception, cause);
  };
})

does not help at all. In the error handler I already receive the "wrapped" error.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
228 views
Welcome To Ask or Share your Answers For Others

1 Answer

This has been fixed with fix($q): Add traceback to unhandled promise rejections -- Commit 316f60f and the fix is included in the v1.6.1 release.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...