Adds error handling to basetar#respond.

This commit is contained in:
Edgar Silva 2015-03-27 15:18:45 -06:00
parent 527b0febed
commit ec4dde7448
3 changed files with 25 additions and 7 deletions

BIN
lib/.basestar.js.swp Normal file

Binary file not shown.

View File

@ -40,13 +40,17 @@ Basestar.prototype.proxyMethods = Utils.proxyFunctionsToObject;
// ...data - additional arguments to be passed to both event/callback
//
// Returns nothing
Basestar.prototype.respond = function(event, callback) {
var args = Array.prototype.slice.call(arguments, 2);
Basestar.prototype.respond = function(event, callback, err) {
var args = Array.prototype.slice.call(arguments, 3);
if (!!err) {
this.emit('error', err);
} else {
this.emit.apply(this, [event].concat(args));
}
if (typeof callback === "function") {
callback.apply(this, args);
callback.apply(this, [err].concat(args));
}
};

View File

@ -52,7 +52,6 @@ describe("Basestar", function() {
describe("#respond", function() {
var listener, callback, child;
var Child = function Child() {};
Utils.subclass(Child, Basestar);
@ -65,16 +64,31 @@ describe("Basestar", function() {
child.on("event", listener);
child.respond("event", callback, "arg1", 2, { three: true });
child.respond("event", callback, null, "arg1", 2, { three: true });
});
it("triggers the callback with all additional arguments", function() {
expect(callback).to.be.calledWith("arg1", 2, { three: true });
expect(callback).to.be.calledWith(null, "arg1", 2, { three: true });
});
it("emits an event with all additional arguments", function() {
expect(listener).to.be.calledWith("arg1", 2, { three: true });
});
context("when err is not null", function() {
var errListener, err;
beforeEach(function() {
errListener = spy();
child = new Child();
child.on("error", errListener);
child.respond("event", callback, "Error on event!", "arg1", 2, { three: true });
});
it ("emits an error event", function() {
expect(errListener).to.be.calledWith("Error on event!");
});
});
});
describe("#defineEvent", function() {