Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
fix($q): reject should catch & forward exceptions thrown in errback
Browse files Browse the repository at this point in the history
  • Loading branch information
IgorMinar committed Aug 24, 2013
1 parent 80d0f98 commit 5d9f420
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/ng/q.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,12 @@ function qFactory(nextTick, exceptionHandler) {
then: function(callback, errback) {
var result = defer();
nextTick(function() {
result.resolve((isFunction(errback) ? errback : defaultErrback)(reason));
try {
result.resolve((isFunction(errback) ? errback : defaultErrback)(reason));
} catch(e) {
result.reject(e);
exceptionHandler(e);
}
});
return result.promise;
}
Expand Down
20 changes: 19 additions & 1 deletion test/ng/qSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -950,6 +950,15 @@ describe('q', function() {
syncResolve(deferred, rejectedPromise.then());
expect(log).toEqual(['error(rejected)->reject(rejected)']);
});


it('should catch exceptions thrown in errback and forward them to derived promises', function() {
var rejectedPromise = q.reject('rejected');
rejectedPromise.then(null, error('Broken', 'catch me!', true)).
then(null, error('Affected'))
mockNextTick.flush();
expect(log).toEqual(['errorBroken(rejected)->throw(catch me!)', 'errorAffected(catch me!)->reject(catch me!)']);
});
});


Expand Down Expand Up @@ -1460,14 +1469,23 @@ describe('q', function() {
deferred = q.defer();
});


afterEach(function() {
// Restore the original exception logging mode
mockNextTick.logExceptions = originalLogExceptions;
});


it('should still reject the promise, when exception is thrown in success handler, even if exceptionHandler rethrows', function() {
deferred.promise.then(function() { throw 'reject'; }).then(null, errorSpy);
deferred.resolve('resolve');
mockNextTick.flush();
expect(exceptionExceptionSpy).toHaveBeenCalled();
expect(errorSpy).toHaveBeenCalled();
});


it('should still reject the promise, when exception is thrown in error handler, even if exceptionHandler rethrows', function() {
deferred.promise.then(null, function() { throw 'reject again'; }).then(null, errorSpy);
deferred.reject('reject');
mockNextTick.flush();
Expand Down

0 comments on commit 5d9f420

Please sign in to comment.