From 78815a199c7bbb2e9dfa73d0540bdde815d6edb3 Mon Sep 17 00:00:00 2001 From: "Nathan G. Romano" Date: Fri, 25 Jul 2014 11:56:15 -0400 Subject: [PATCH] added an error method to the API --- README.md | 8 ++++++++ lib/builder.js | 1 + lib/controller.js | 21 ++++++++++++++++++++- package.json | 2 +- spec/lib/controller-spec.coffee | 9 +++++++++ 5 files changed, 39 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index bf6b1bf..657b8d9 100644 --- a/README.md +++ b/README.md @@ -477,6 +477,14 @@ to the socket will be halted. controller.consume(); ``` +### Controller#errored(err:Error) + +When processing a message you decide that you want to *respond* with an error simply call `errored`. + +```javascript +controller.errored(new Error('Some Error')); +``` + # Installation and Environment Setup Install node.js (See download and install instructions here: http://nodejs.org/). diff --git a/lib/builder.js b/lib/builder.js index 0ab5b96..14e872c 100644 --- a/lib/builder.js +++ b/lib/builder.js @@ -33,6 +33,7 @@ util.inherits(Builder, events.EventEmitter); 'actor action target content id created reference published'.split(' ').forEach(function (name) { Builder.prototype[name] = function () { + debug('delegating %s to the message instance', name); var v = this.message[name].apply(this.message,slice.call(arguments)); if ('object' === typeof v && (v === this.message || v.isMessage)) { return this; diff --git a/lib/controller.js b/lib/controller.js index cc5678b..e8733fa 100644 --- a/lib/controller.js +++ b/lib/controller.js @@ -99,14 +99,33 @@ Controller.prototype.deliver = function () { return this; }; + +/** + * This method is a conveince for setting the content and as well as triggering + * the response, if we encounter an error. + * + * @param {mixed} content + * @return Controller + */ + +Controller.prototype.errored = function (err) { + debug('responding with an error'); + this.action(this.action() + ' errored').respond(err); + return this; +}; + + + + /** * set up delegates */ 'actor action target content id created reference published'.split(' ').forEach(function (name) { Controller.prototype[name] = function () { + debug('delegating %s to the message instance', name); var v = this.message[name].apply(this.message,slice.call(arguments)); - if (v === this.message) { + if ('object' === typeof v && (v === this.message || v.isMessage)) { return this; } else { diff --git a/package.json b/package.json index fd10255..9a200f2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bus.io-common", - "version": "0.1.2", + "version": "0.2.0", "description": "The common components for bus.io", "main": "index.js", "scripts": { diff --git a/spec/lib/controller-spec.coffee b/spec/lib/controller-spec.coffee index e53aade..f631b38 100644 --- a/spec/lib/controller-spec.coffee +++ b/spec/lib/controller-spec.coffee @@ -107,6 +107,15 @@ describe 'Controller', -> And -> expect(@controller.emit.argsForCall[2][1].created() instanceof Date).toBe true And -> expect(@controller.message.delivered instanceof Date).toBe true + describe.only '#errored', -> + + Given -> @err = 'Some Error' + Given -> spyOn(@controller,'action').andCallThrough() + Given -> spyOn(@controller,'respond') + When -> @controller.errored @err + Then -> expect(@controller.action).toHaveBeenCalledWith 'say errored' + And -> expect(@controller.respond).toHaveBeenCalledWith @err + describe '#actor (v:String="a")', -> Given -> @v = 'a'