Skip to content

Commit

Permalink
Add warning to then()
Browse files Browse the repository at this point in the history
When debugging, warn when then is passed something
but nothing is a function
  • Loading branch information
petkaantonov committed Feb 2, 2015
1 parent 06146b8 commit c352c37
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 5 deletions.
3 changes: 1 addition & 2 deletions src/call_get.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,11 @@ getGetter = function(name) {
};
}

var classString= {}.toString;
function ensureMethod(obj, methodName) {
var fn;
if (obj != null) fn = obj[methodName];
if (typeof fn !== "function") {
var message = "Object " + classString.call(obj) + " has no method '" +
var message = "Object " + util.classString(obj) + " has no method '" +
util.toString(methodName) + "'";
throw new Promise.TypeError(message);
}
Expand Down
13 changes: 13 additions & 0 deletions src/debuggability.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use strict";
module.exports = function(Promise, CapturedTrace) {
var async = require("./async.js");
var Warning = require("./errors.js").Warning;
var util = require("./util.js");
var ASSERT = require("./assert.js");
var canAttachTrace = util.canAttachTrace;
Expand Down Expand Up @@ -110,6 +111,18 @@ Promise.prototype._attachExtraTrace = function (error, ignoreSelf) {
}
};

Promise.prototype._warn = function(message) {
var warning = new Warning(message);
var ctx = this._peekContext();
if (ctx) {
ctx.attachExtraTrace(warning);
} else {
var parsed = CapturedTrace.parseStackAndMessage(warning);
warning.stack = parsed.message + "\n" + parsed.stack.join("\n");
}
CapturedTrace.formatAndLogError(warning, "");
};

Promise.onPossiblyUnhandledRejection = function (fn) {
possiblyUnhandledRejection = typeof fn === "function" ? fn : undefined;
};
Expand Down
6 changes: 5 additions & 1 deletion src/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ function subError(nameProperty, defaultMessage) {
notEnumerableProp(this, "name", nameProperty);
if (Error.captureStackTrace) {
Error.captureStackTrace(this, this.constructor);
} else {
Error.call(this);
}
}
inherits(SubError, Error);
return SubError;
}

var _TypeError, _RangeError;
var Warning = subError("Warning", "warning");
var CancellationError = subError("CancellationError", "cancellation error");
var TimeoutError = subError("TimeoutError", "timeout error");
var AggregateError = subError("AggregateError", "aggregate error");
Expand Down Expand Up @@ -98,5 +101,6 @@ module.exports = {
CancellationError: errorTypes.CancellationError,
OperationalError: errorTypes.OperationalError,
TimeoutError: errorTypes.TimeoutError,
AggregateError: errorTypes.AggregateError
AggregateError: errorTypes.AggregateError,
Warning: Warning
};
11 changes: 10 additions & 1 deletion src/promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,20 @@ Promise.prototype.reflect = function () {
};

Promise.prototype.then = function (didFulfill, didReject, didProgress) {
if (isDebugging() && arguments.length > 0 &&
typeof didFulfill !== "function" &&
typeof didReject !== "function") {
var msg = ".then() only accepts functions but was passed: " +
util.classString(didFulfill);
if (arguments.length > 1) {
msg += ", " + util.classString(didReject);
}
this._warn(msg);
}
return this._then(didFulfill, didReject, didProgress,
undefined, undefined);
};


Promise.prototype.done = function (didFulfill, didReject, didProgress) {
var promise = this._then(didFulfill, didReject, didProgress,
undefined, undefined);
Expand Down
7 changes: 6 additions & 1 deletion src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,10 @@ var ensureErrorObject = (function() {
}
})();

function classString(obj) {
return {}.toString.call(obj);
}

var ret = {
isClass: isClass,
isIdentifier: isIdentifier,
Expand All @@ -264,7 +268,8 @@ var ret = {
canAttachTrace: canAttachTrace,
ensureErrorObject: ensureErrorObject,
originatesFromRejection: originatesFromRejection,
markAsOriginatingFromRejection: markAsOriginatingFromRejection
markAsOriginatingFromRejection: markAsOriginatingFromRejection,
classString: classString
};
try {throw new Error(); } catch (e) {ret.lastLineError = e;}
module.exports = ret;

0 comments on commit c352c37

Please sign in to comment.