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.
There is another case, adding a
finally()
handler to a promise generate the error: http://plnkr.co/edit/eT834BkIEooAMvrVcLDeBecause
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.
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); }]);
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: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:The offending code is the frame calling angular's catch/then functions.
First option is simply to hide an error with disablinconfiguring
errorOnUnhandledRejections
in $qProvider configuratio as suggested Cengkuru Michael: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:Useful Links:
I have solved this error by adding a default value in the catch block like:
(take in count that I am not an experienced angular developer)
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 !