Possibly unhandled rejection in Angular 1.6

Piotr Pradzynski picture Piotr Pradzynski · Dec 22, 2016 · Viewed 51.8k times · Source

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.

Answer

gkalpak picture gkalpak · Dec 22, 2016

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.