Skip to content

Commit

Permalink
Merge pull request kriskowal#634 from grahamrhay/fin-callback
Browse files Browse the repository at this point in the history
Better error when finally callback is invalid
  • Loading branch information
kriskowal committed May 20, 2015
2 parents d373079 + 79e5b71 commit fe53a99
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
- Throw if callback supplied to "finally" is invalid (@grahamrhay)

## 1.4.1

Expand Down
3 changes: 3 additions & 0 deletions q.js
Original file line number Diff line number Diff line change
Expand Up @@ -1752,6 +1752,9 @@ Q["finally"] = function (object, callback) {

Promise.prototype.fin = // XXX legacy
Promise.prototype["finally"] = function (callback) {
if (!callback || typeof callback.apply !== "function") {
throw new Error("Can't apply finally callback");
}
callback = Q(callback);
return this.then(function (value) {
return callback.fcall().then(function () {
Expand Down
32 changes: 32 additions & 0 deletions spec/q-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1671,6 +1671,38 @@ describe("fin", function () {

});

describe("when the callback is invalid", function () {
describe("undefined", function () {
it("should have a useful error", function () {
var foo = {},
threw = false;

try {
Q().fin(foo.bar);
} catch (err) {
expect(err.message).toBe("Can't apply finally callback");
threw = true;
}

expect(threw).toBe(true);
});
});

describe("not a function", function () {
it("should have a useful error", function () {
var threw = false;

try {
Q().fin(123);
} catch (err) {
expect(err.message).toBe("Can't apply finally callback");
threw = true;
}

expect(threw).toBe(true);
});
});
});
});

// Almost like "fin"
Expand Down

0 comments on commit fe53a99

Please sign in to comment.