Possibly unhandled rejection in Angular 1.6
Asked Answered
T

9

33

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.

Tui answered 22/12, 2016 at 10:56 Comment(2)
No idea about this. But I recently reverted my application from 1.6 to 1.5.x.. I was having lots of issues with 1.6.Biamonte
I'm about to revert as well, especially after seeing the answer below. I wasted two days on this nonsense.Circle
K
20

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.

Kynewulf answered 22/12, 2016 at 12:28 Comment(7)
Discussion on this also buried in an old, unrelated issue: github.com/angular/angular.js/issues/14631Circle
In 1.6.1, I was still getting this error, using ngResource and $promise, so I had to use: $qProvider.errorOnUnhandledRejections(false);Bomb
@httpete: OP asked how to get a more clear error, not how to suppress unhandled rejections (which is not a good idea imo).Kynewulf
Well, i am with 1.6.1 and i still get the error. Even if i do have an error callback. Problem is my callback need to return a rejected promise so the client knows the service have an error. Then Poof! $http(config).then( (response) => {...}, (error) => { return $q.reject(error); } );Hygroscope
If you don't handle a rejection, you get an error. That's how it works. (Note that each then() call returns a new promise.)Kynewulf
Well, this means i have to do same thing as @httpete, or its a no go sadly :(. We have an interceptor wich deals with unhandled promises so that we dont need to do the same code everywhere.Hygroscope
I get this error when moving from 1.5 to 1.7, and I do not have any (explicit) promises in my code. It functions perfectly in 1.5, but throws that error 3 times on starting in 1.7Runesmith
K
16

First option simply is to hide this specific rejection error by setting errorOnUnhandledRejections in $qProvider configuration as suggested Cengkuru Michael:

app.config(['$qProvider', function ($qProvider) {
    $qProvider.errorOnUnhandledRejections(false);
}]);

BUT this will only switch off logging. The error itself will remain

The better solution in this case will be - handling a rejection with .catch() method:

service.doSomething()
    .then(function (response) {})
    .catch(function (err) {});

Useful Links:

Kev answered 28/7, 2017 at 8:28 Comment(0)
T
9

I fixed the same problem with version 1.6.1 by upgrading angular-ui-router to 0.3.2.

Tensive answered 9/1, 2017 at 11:29 Comment(1)
0.4 fixes some bugs also.Appendix
B
8

This information helped me to track down what (in my case) was creating the promise and not adding an error handler. I found it buried in the discussion of issue #2889 "Possibly unhandled rejection with Angular 1.5.9".

The gist, is, patch $q to cache a stack-trace on creating promises, such that it can be retrieved when the error is triggered.

To do it, insert this code to decorate $q somewhere near the top of your angular app:

// Decorate the $q service when app starts
app.decorator('$q', ["$delegate", function($delegate) {
  // Create a new promise object
  var promise = $delegate.when();

  // Access the `Promise` prototype (nonstandard, but works in Chrome)
  var proto = promise.__proto__;

  // Define a setter for `$$state` that creates a stacktrace 
  // (string) and assigns it as a property of the internal `$$state` object.
  Object.defineProperty(proto, '$$state', {
    enumerable: true,
    set: function(val) {
      val.stack = new Error().stack;
      this._$$state = val;
    },
    get: function() {
      return this._$$state;
    }
  });

  return $delegate;
}]);

Then search the angular code for the message "possibly unhandled rejection" and put a breakpoint on that line. When the breakpoint is reached, print out the value of toCheck.stack on the console, and you'll see something like this:

>> toCheck.stack
"set@http://localhost:8000/js/dual-site.js:18:19
Promise@http://localhost:8000/js/angular.js:17008:22
then@http://localhost:8000/js/angular.js:17016:20
catch@http://localhost:8000/js/angular.js:17026:14
SyncStrategy.prototype.send@http://localhost:8000/js/angular-state-machine.js:436:24
StateMachine/this.send@http://localhost:8000/js/angular-state-machine.js:235:16

The offending code is the frame calling angular's catch/then functions.

Backandforth answered 18/10, 2017 at 16:59 Comment(1)
This should be the right answer, and this should be part of Angular. Thanks a lot!!Indamine
H
4

There is another case, adding a finally() handler to a promise generate the error: http://plnkr.co/edit/eT834BkIEooAMvrVcLDe

Because finally() creates a new promise and call the resolver on it. (Rejecting a 2nd one in a rejection case)

Ive put a fix in the plnkr but it doesn't look very good.

Hygroscope answered 13/1, 2017 at 16:11 Comment(0)
C
3

I got same unhandled rejection error when a rejected promise is not handled by angular-ui-router (ui-sref) using angular ver1.6.1 & This feature is enabled by default.

For anyone that wants a workaround (not recommended, though), you can globally silence unhandled promise rejections like this —

app.config(['$qProvider', function ($qProvider) { $qProvider.errorOnUnhandledRejections(false); }]);

Crackling answered 18/1, 2017 at 6:20 Comment(1)
It is not good solution. I tested it and in some situations this setting will hide the true error as well.Tui
C
1

I have solved this error by adding a default value in the catch block like:

service.doSomething()
    .then(function(response) {
        var x = null;
        var y = x.y;
    }).catch(function(error) {
        var y = 0;
    });

(take in count that I am not an experienced angular developer)

Cambodia answered 16/7, 2017 at 14:49 Comment(0)
J
0

I have the problem even with version 1.6.1 in my httpErrorInterceptor, for one usecase my if my api return 404 i have to try another request with other data... so in this case i only reject the request and angular throw the unhandled rejection error...

I install 1.5.9 and now there is no more error !

Josefjosefa answered 5/1, 2017 at 8:58 Comment(0)
S
0

errorOnUnhandledRejections(false); was not a resolution for me.

You do indeed need to define an exception handler... however... wrap it in a timeout function: this will force the original exception/stack trace to be thrown.

To make the error show up as an error in the web console, as you originally intended:

ng.factory('$exceptionHandler', function($log) {
  return function(exception, cause) {
    // do some some stuff...
    setTimeout(function(){
      // throw the original exception (with correct line #'s etc)
      throw exception;
    })
  };
});

Heres the timeout trick: Why can I not throw inside a Promise.catch handler?

Strait answered 10/5, 2017 at 22:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.