Skip to content
This repository has been archived by the owner on Mar 24, 2020. It is now read-only.

Commit

Permalink
Altered caught exception reporting; now must be explicit. Provided be…
Browse files Browse the repository at this point in the history
…tter .end behavior and added .report chaining.
  • Loading branch information
kriskowal committed Jun 10, 2011
1 parent a6e9060 commit c480204
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
12 changes: 12 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# vim:ts=4:sts=4:sw=4:et:tw=60

Next
- ``end`` no longer returns a promise. It is the end of the
promise chain.
- Stopped reporting thrown exceptions in ``when`` callbacks
and errbacks. These must be explicitly reported through
``.end()``, ``.then(null, Q.error)``, or some other
mechanism.
- Added ``report`` as an API method, which can be used as
an errback to report and propagate an error.
- Added ``report`` as a promise-chain method, so an error
can be reported if it passes such a gate.

0.4.3
- Fixed <script> support that regressed with 0.4.2 because
of "use strict" in the module system multi-plexer.
Expand Down
51 changes: 43 additions & 8 deletions q.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,20 @@ Promise.prototype.then = function (fulfilled, rejected) {
};

Promise.prototype.end = function () {
return when(this, undefined, error);
when(this, undefined, error);
};

Promise.prototype.report = function (error) {
if (!error) {
error = new Error("REPORT");
} else if (!error.message) {
error = new Error(error || "REPORT");
}
return when(this, undefined, function (reason) {
report(error);
report(reason);
return reject(reason);
});
};

Promise.prototype.toSource = function () {
Expand Down Expand Up @@ -357,11 +370,6 @@ function when(value, fulfilled, rejected) {
try {
return fulfilled ? fulfilled(value) : value;
} catch (exception) {
if (typeof process !== "undefined") {
process.emit('uncaughtException', exception);
} else {
print(exception && exception.stack || exception);
}
return reject(exception);
}
}
Expand All @@ -370,7 +378,6 @@ function when(value, fulfilled, rejected) {
try {
return rejected ? rejected(reason) : reject(reason);
} catch (exception) {
print(exception && exception.stack || exception);
return reject(exception);
}
}
Expand Down Expand Up @@ -529,9 +536,37 @@ exports.keys = Method("keys");
*/
exports.error = error;
function error(reason) {
throw reason;
if (typeof process !== "undefined") {
report(reason);
process.exit(reason ? reason.exit || 1 : 1);
} else {
report(reason);
throw reason;
}
};

/**
* Reports an error and returns it as a rejection.
*
* Q.when(function () {
* if (failure) {
* throw new Error();
* } else {
* return success;
* }
* })
* // report the error
* .then(null, Q.report)
* .then(function (success) {
* // carry on if no failure
* });
*/
exports.report = report;
function report(reason) {
print(reason && reason.stack || reason);
return reject(reason);
}

/*
* Enqueues a promise operation for a future turn.
*/
Expand Down
26 changes: 26 additions & 0 deletions ref_send.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

This API varies from Tyler Closes ref_send in the
following ways:

* Promises can be resolved to function values.
* Promises can be resolved to null or undefined.
* Promises are distinguishable from arbitrary functions.
* The promise API is abstracted with a Promise constructor
that accepts a descriptor that receives all of the
messages forwarded to that promise and handles the
common patterns for message receivers. The promise
constructor also takes optional fallback and valueOf
methods which handle the cases for missing handlers on
the descriptor (rejection by default) and the valueOf
call (which returns the promise itself by default)
* near(ref) has been changed to Promise.valueOf() in
keeping with JavaScript's existing Object.valueOf().
* post(promise, name, args) has been altered to a variadic
post(promise, name ...args)
* variadic arguments are used internally where
applicable. However, I have not altered the Q.post()
API to expand variadic arguments since Tyler Close
informed the CommonJS list that it would restrict
usage patterns for web_send, posting arbitrary JSON
objects as the "arguments" over HTTP.

0 comments on commit c480204

Please sign in to comment.