Skip to content

Commit

Permalink
Error in progress handlers stops propagation and calls onerror.
Browse files Browse the repository at this point in the history
Closes #136.
  • Loading branch information
domenic committed Nov 25, 2012
1 parent 24d98cf commit 2bc115b
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
17 changes: 16 additions & 1 deletion q.js
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,22 @@ function when(value, fulfilled, rejected, progressed) {
resolvedValue.promiseDispatch(void 0, "when", [
void 0,
function (value) {
deferred.notify(_progressed(value));
var newValue;
var threw = false;
try {
newValue = _progressed(value);
} catch (e) {
threw = true;
if (Q.onerror) {
Q.onerror(e);
} else {
throw e;
}
}

if (!threw) {
deferred.notify(newValue);
}
}
]);

Expand Down
46 changes: 46 additions & 0 deletions spec/q-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,25 @@ describe("progress", function () {
return promise;
});

it("should re-throw all errors thrown by listeners to Q.onerror", function () {
var theError = new Error("boo!");

var def = Q.defer();
def.promise.progress(function () {
throw theError;
});

var deferred = Q.defer();
Q.onerror = function (error) {
expect(error).toBe(theError);
deferred.resolve();
};
Q.delay(100).then(deferred.reject);

def.notify();

return deferred.promise;
});
});

describe("promises for objects", function () {
Expand Down Expand Up @@ -928,6 +947,33 @@ describe("propagation", function () {

return promise;
});


it("should stop progress propagation if an error is thrown", function () {
var def = Q.defer();
var p2 = def.promise.progress(function () {
throw new Error("boo!");
});

Q.onerror = function () { /* just swallow it for this test */ };

var progressValues = [];
var result = p2.then(
function () {
expect(progressValues).toEqual([]);
},
function () {
expect(true).toBe(false);
},
function (progressValue) {
progressValues.push(progressValue);
}
);

def.notify();
def.fulfill();
return result;
});
});

describe("all", function () {
Expand Down

0 comments on commit 2bc115b

Please sign in to comment.