From ec4dde7448c2210688cda67f5677bd1b4ad324de Mon Sep 17 00:00:00 2001 From: Edgar Silva Date: Fri, 27 Mar 2015 15:18:45 -0600 Subject: [PATCH] Adds error handling to basetar#respond. --- lib/.basestar.js.swp | Bin 0 -> 12288 bytes lib/basestar.js | 12 ++++++++---- spec/lib/basestar.spec.js | 20 +++++++++++++++++--- 3 files changed, 25 insertions(+), 7 deletions(-) create mode 100644 lib/.basestar.js.swp diff --git a/lib/.basestar.js.swp b/lib/.basestar.js.swp new file mode 100644 index 0000000000000000000000000000000000000000..0026ea047ffc19fdb5a1a1d6e23cd5e0f53a0399 GIT binary patch literal 12288 zcmeHNONbmr7%q+PH}MI2_%j|d8)kbpiK1p*C9d&-Pc#uR#85L`v(rg$cUx7xo58Gl z@p(~Co>lZFD2fL`5m6)J)vJPd5XFNhMTq$QRn541!tQde51T^v zRkuG%^Q@aDy>3q$WvtZEA+w&A=6SWS7-$%1 z7-$%17-$%17-$%17-$%17-$%17?80LpMZup_zwX1|9|!S|MWR^VEo1^jug5Z?e_0bc;0 z1IvH}9tUm$emzHsPl4Bf6zBpS;0oZ!vxWE&C;6BEU1kYsAllFDp5FRYoG6_DVw2yc)q zC`)tXPk5xeZ}L+274G|!dI0Vmj1u#()k(JCmC>#ABq>&Yzg$$i+<00ce=;j>t6o$o zbZ&DyDpYh3r$LOLh)YXMUxKHObVCE3FOm#eT6i63 zqjcqZeYSSTvatDv>B{y0WGhf4{q)RHD-f9L7PYk+D6KMtDg>874mi=Yn8#(RC;@?e zTgo&YA)3;|2-%AA;WE49|L~nru<1m`C-TRo%?g5$aICn`Re2Yw#l%LkDAJLO;*gFV zLRNC@@-T82QJI-xGO+DdS;SanD?CjGd?#->caj5CzP2ehIU;#`D!IRb$7z5rz$9YSh2f49CT~)k1|D7DAs;B_^hs zMw}|7y{24KmRk-`%25ALoQ$HqUID^rJO?&e7q=6!wVc305W1Aa2xeXz+V_)N8kdjvA+7V6Tbg|~ zNe%nq$movH-I%@k11^1M*@|&%q3Cdmeu0aE#x90-U@@o}$|VK5R2DXTUMi0rKKAV%fVbVfV;Ey+t)Q ztL)&emiog4rO}zotsT7Gmxb)1QQ?qfgV|v&cG*d+(|3j@V&x$S4OAh@OQQy5N*j9A%R3W79zjpcv6m4CjxzxEYfjsFe%ZY0ayTVot~Nvm!I)N0 zl!;dDR@5yy*1JQM7D^lLOPFovoi*fIQyu&ahQOqkCguF{TT{`b0KC|Op=6NEecQt2 zwyvOUbT4|0j2BR5v1^VM?Z$iZ@X(HEhPVU4(}*SK^J0XAA6jbnr)bB__MOxCxSsYx j_O4M+Co$cr^Rj>}i@W#i4;kzLi+sr}^UL<4yG{HJ-AvtN literal 0 HcmV?d00001 diff --git a/lib/basestar.js b/lib/basestar.js index ff49806..e6794c7 100644 --- a/lib/basestar.js +++ b/lib/basestar.js @@ -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); - this.emit.apply(this, [event].concat(args)); + 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)); } }; diff --git a/spec/lib/basestar.spec.js b/spec/lib/basestar.spec.js index 769df22..10511a5 100644 --- a/spec/lib/basestar.spec.js +++ b/spec/lib/basestar.spec.js @@ -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() {