Permalink
Browse files

Altered caught exception reporting; now must be explicit. Provided be…

…tter .end behavior and added .report chaining.
  • Loading branch information...
1 parent a6e9060 commit c48020451a79ec4b9110896549e4a2b05ebf177b @kriskowal committed Jun 10, 2011
Showing with 82 additions and 8 deletions.
  1. +1 −0 .gitignore
  2. +12 −0 CHANGES
  3. +43 −8 q.js
  4. +26 −0 ref_send.md
View
@@ -0,0 +1 @@
+node_modules
View
@@ -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.
View
@@ -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 () {
@@ -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);
}
}
@@ -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);
}
}
@@ -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.
*/
View
@@ -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.