We have a pattern for resolving promises in our Angular app that has served us well up until Angular 1.6.0:
resource.get().$promise
.then(function (response) {
// do something with the response
}, function (error) {
// pass the error the the error service
return errorService.handleError(error);
});
And here is how we are triggering the error in Karma:
resourceMock.get = function () {
var deferred = $q.defer();
deferred.reject(error);
return { $promise: deferred.promise };
};
Now, with the update to 1.6.0, Angular is suddenly complaining in our unit tests (in Karma) for rejected promises with a "Possibly unhandled rejection" error. But we are handling the rejection in the second function that calls our error service.
What exactly is Angular looking for here? How does it want us to "handle" the rejection?
You could mask the problem by turning off errorOnUnhandledRejections, but the error says you're needing to "handle a possible rejection" so you just need to add a catch to your promise.
Reference: https://github.com/angular-ui/ui-router/issues/2889
To avoid having to type additional
.catch(function () {})
in your code in multiple places, you could add adecorator
to the$exceptionHandler
.This is a more verbose option than the others but you only have to make the change in one place.
It may not be your speficic situation, but I had a similar problem.
In my case, I was using angular-i18n, and getting the locale dictionary asynchronously. The problem was that the json file it was getting was incorrectly indented (mixing spaces and tabs). The GET request didn't failed.
Correcting the indentation solved the problem.
The code you show will handle a rejection that occurs before the call to
.then
. In such situation, the 2nd callback you pass to.then
will be called, and the rejection will be handled.However, when the promise on which you call
.then
is successful, it calls the 1st callback. If this callback throws an exception or returns a rejected promise, this resulting rejection will not be handled, because the 2nd callback does not handle rejections in cause by the 1st. This is just how promise implementations compliant with the Promises/A+ specification work, and Angular promises are compliant.You can illustrate this with the following code:
If you run it in Node, which also conforms to Promises/A+, you get:
Found the issue by rolling back to Angular 1.5.9 and rerunning the test. It was a simple injection issue but Angular 1.6.0 superseded this by throwing the "Possibly Unhandled Rejection" error instead, obfuscating the actual error.
I had this same notice appear after making some changes. It turned out to be because I had changed between a single
$http
request to multiple requests using angularjs$q
service.I hadn't wrapped them in an array. e.g.
rather than
I hope this might save somebody some time.