From b9d65b5178d4c823ad41122ed067b00eb924ecbd Mon Sep 17 00:00:00 2001 From: fschoenfelder Date: Wed, 2 Jul 2014 10:11:02 +0200 Subject: [PATCH 01/17] forward request errors to model caller. --- lib/proto.js | 9 ++++++--- lib/static.js | 5 ++++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/proto.js b/lib/proto.js index 0d439d7..616dc2b 100644 --- a/lib/proto.js +++ b/lib/proto.js @@ -144,7 +144,8 @@ exports.destroy = function(fn){ this.request .del(url) .set(this.model._headers) - .end(function(res){ + .end(function(err, res){ + if (err) return fn(err, null); if (res.error) return fn(error(res), res); self.destroyed = true; self.model.emit('destroy', self, res); @@ -178,7 +179,8 @@ exports.save = function(fn){ .post(url) .set(this.model._headers) .send(self) - .end(function(res){ + .end(function(err, res){ + if (err) return fn(err, null); if (res.error) return fn(error(res), res); if (res.body) self.primary(res.body[key]); self.dirty = {}; @@ -206,7 +208,8 @@ exports.update = function(fn){ .put(url) .set(this.model._headers) .send(self) - .end(function(res){ + .end(function(err, res){ + if (err) return fn(err, null); if (res.error) return fn(error(res), res); self.dirty = {}; self.model.emit('save', self, res); diff --git a/lib/static.js b/lib/static.js index 55923da..74a6dd8 100644 --- a/lib/static.js +++ b/lib/static.js @@ -146,7 +146,8 @@ exports.destroyAll = function(fn){ this.request .del(url) .set(this._headers) - .end(function(res){ + .end(function(err, res){ + if (err) return fn(err, null, null); if (res.error) return fn(error(res), null, res); fn(null, [], res); }); @@ -166,6 +167,7 @@ exports.all = function(fn){ .get(url) .set(this._headers) .end(function(res){ + if (err) return fn(err, null, null); if (res.error) return fn(error(res), null, res); var col = new Collection; for (var i = 0, len = res.body.length; i < len; ++i) { @@ -190,6 +192,7 @@ exports.get = function(id, fn){ .get(url) .set(this._headers) .end(function(res){ + if (err) return fn(err, null, null); if (res.error) return fn(error(res), null, res); var model = new self(res.body); fn(null, model, res); From d58a67829a1f111bae53f0b2e47ca51096de5e83 Mon Sep 17 00:00:00 2001 From: fschoenfelder Date: Wed, 2 Jul 2014 11:31:46 +0200 Subject: [PATCH 02/17] export error functions --- lib/proto.js | 21 ++++++++++++--------- lib/static.js | 23 +++++++++++++---------- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/lib/proto.js b/lib/proto.js index 616dc2b..788f610 100644 --- a/lib/proto.js +++ b/lib/proto.js @@ -145,8 +145,8 @@ exports.destroy = function(fn){ .del(url) .set(this.model._headers) .end(function(err, res){ - if (err) return fn(err, null); - if (res.error) return fn(error(res), res); + if (err || res.error) + return fn(error(err,res), res); self.destroyed = true; self.model.emit('destroy', self, res); self.emit('destroy'); @@ -180,8 +180,9 @@ exports.save = function(fn){ .set(this.model._headers) .send(self) .end(function(err, res){ - if (err) return fn(err, null); - if (res.error) return fn(error(res), res); + if (err || res.error) + return fn(error(err,res), res); + if (res.body) self.primary(res.body[key]); self.dirty = {}; self.model.emit('save', self, res); @@ -208,9 +209,9 @@ exports.update = function(fn){ .put(url) .set(this.model._headers) .send(self) - .end(function(err, res){ - if (err) return fn(err, null); - if (res.error) return fn(error(res), res); + .end(function(err,res){ + if (err || res.error) + return fn(error(err, res), res); self.dirty = {}; self.model.emit('save', self, res); self.emit('save'); @@ -298,6 +299,8 @@ exports.toJSON = function(){ * @api private */ -function error(res) { - return new Error('got ' + res.status + ' response'); +function error(err, res) { + if (err) return err + else return new Error('got ' + res.status + ' response'); } +exports.error = error diff --git a/lib/static.js b/lib/static.js index 74a6dd8..d185907 100644 --- a/lib/static.js +++ b/lib/static.js @@ -147,8 +147,8 @@ exports.destroyAll = function(fn){ .del(url) .set(this._headers) .end(function(err, res){ - if (err) return fn(err, null, null); - if (res.error) return fn(error(res), null, res); + if (err || res.error) + return fn(error(err,res), null, res); fn(null, [], res); }); }; @@ -166,9 +166,9 @@ exports.all = function(fn){ this.request .get(url) .set(this._headers) - .end(function(res){ - if (err) return fn(err, null, null); - if (res.error) return fn(error(res), null, res); + .end(function(err, res){ + if (err || res.error) + return fn(error(err,res), null, res); var col = new Collection; for (var i = 0, len = res.body.length; i < len; ++i) { col.push(new self(res.body[i])); @@ -191,9 +191,9 @@ exports.get = function(id, fn){ this.request .get(url) .set(this._headers) - .end(function(res){ - if (err) return fn(err, null, null); - if (res.error) return fn(error(res), null, res); + .end(function(err, res){ + if (err || res.error) + return fn(error(err,res), null, res); var model = new self(res.body); fn(null, model, res); }); @@ -207,6 +207,9 @@ exports.get = function(id, fn){ * @api private */ -function error(res) { - return new Error('got ' + res.status + ' response'); +function error(err, res) { + if (err) return err; + else return new Error('got ' + res.status + ' response'); } + +export.error = error From 5c11dca6ed423709daaea56b8f4119888a95978d Mon Sep 17 00:00:00 2001 From: fschoenfelder Date: Wed, 2 Jul 2014 11:36:40 +0200 Subject: [PATCH 03/17] fix naming bug --- lib/static.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/static.js b/lib/static.js index d185907..f3484ef 100644 --- a/lib/static.js +++ b/lib/static.js @@ -212,4 +212,4 @@ function error(err, res) { else return new Error('got ' + res.status + ' response'); } -export.error = error +exports.error = error From 635322f70b94efc95921271192109f29ecceb423 Mon Sep 17 00:00:00 2001 From: fschoenfelder Date: Wed, 2 Jul 2014 11:43:03 +0200 Subject: [PATCH 04/17] fix js syntax --- lib/proto.js | 4 ++-- lib/static.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/proto.js b/lib/proto.js index 788f610..52f398b 100644 --- a/lib/proto.js +++ b/lib/proto.js @@ -300,7 +300,7 @@ exports.toJSON = function(){ */ function error(err, res) { - if (err) return err + if (err) return err; else return new Error('got ' + res.status + ' response'); } -exports.error = error +exports.error = error; diff --git a/lib/static.js b/lib/static.js index f3484ef..e8e9bdd 100644 --- a/lib/static.js +++ b/lib/static.js @@ -212,4 +212,4 @@ function error(err, res) { else return new Error('got ' + res.status + ' response'); } -exports.error = error +exports.error = error; From 4f91a9edb8e1546b3e087d9dabe902c8aa8fa7ed Mon Sep 17 00:00:00 2001 From: fschoenfelder Date: Wed, 2 Jul 2014 12:52:37 +0200 Subject: [PATCH 05/17] remove error function in proto --- lib/proto.js | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/lib/proto.js b/lib/proto.js index 52f398b..5873397 100644 --- a/lib/proto.js +++ b/lib/proto.js @@ -181,7 +181,7 @@ exports.save = function(fn){ .send(self) .end(function(err, res){ if (err || res.error) - return fn(error(err,res), res); + return fn(self.error(err,res), res); if (res.body) self.primary(res.body[key]); self.dirty = {}; @@ -299,8 +299,3 @@ exports.toJSON = function(){ * @api private */ -function error(err, res) { - if (err) return err; - else return new Error('got ' + res.status + ' response'); -} -exports.error = error; From a2e5e121d00647241dbd4b6d7e2e6cc2854783a8 Mon Sep 17 00:00:00 2001 From: fschoenfelder Date: Wed, 2 Jul 2014 12:56:44 +0200 Subject: [PATCH 06/17] Reverting "remove error function in proto" Revert "remove error function in proto" This reverts commit 4f91a9edb8e1546b3e087d9dabe902c8aa8fa7ed. --- lib/proto.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/proto.js b/lib/proto.js index 5873397..52f398b 100644 --- a/lib/proto.js +++ b/lib/proto.js @@ -181,7 +181,7 @@ exports.save = function(fn){ .send(self) .end(function(err, res){ if (err || res.error) - return fn(self.error(err,res), res); + return fn(error(err,res), res); if (res.body) self.primary(res.body[key]); self.dirty = {}; @@ -299,3 +299,8 @@ exports.toJSON = function(){ * @api private */ +function error(err, res) { + if (err) return err; + else return new Error('got ' + res.status + ' response'); +} +exports.error = error; From 5765e7320437bdb4250309bc0bb9ae69943ea13f Mon Sep 17 00:00:00 2001 From: thrucker Date: Wed, 2 Jul 2014 13:34:48 +0200 Subject: [PATCH 07/17] add opt-in for error handling --- lib/proto.js | 32 ++++++++++---------------------- lib/static.js | 26 ++++++++++++-------------- 2 files changed, 22 insertions(+), 36 deletions(-) diff --git a/lib/proto.js b/lib/proto.js index 52f398b..2d14530 100644 --- a/lib/proto.js +++ b/lib/proto.js @@ -25,23 +25,6 @@ Emitter(exports); */ exports.request = request; -/** - * Register an error `msg` on `attr`. - * - * @param {String} attr - * @param {String} msg - * @return {Object} self - * @api public - */ - -exports.error = function(attr, msg){ - this.errors.push({ - attr: attr, - message: msg - }); - return this; -}; - /** * Check if this model is new. * @@ -146,7 +129,7 @@ exports.destroy = function(fn){ .set(this.model._headers) .end(function(err, res){ if (err || res.error) - return fn(error(err,res), res); + return fn(self._error(err,res), res); self.destroyed = true; self.model.emit('destroy', self, res); self.emit('destroy'); @@ -181,7 +164,7 @@ exports.save = function(fn){ .send(self) .end(function(err, res){ if (err || res.error) - return fn(error(err,res), res); + return fn(self._error(err,res), res); if (res.body) self.primary(res.body[key]); self.dirty = {}; @@ -211,7 +194,7 @@ exports.update = function(fn){ .send(self) .end(function(err,res){ if (err || res.error) - return fn(error(err, res), res); + return fn(self._error(err, res), res); self.dirty = {}; self.model.emit('save', self, res); self.emit('save'); @@ -299,8 +282,13 @@ exports.toJSON = function(){ * @api private */ -function error(err, res) { +exports.error = function(fn) { + if (fn) { + this._error = fn; + } +} + +exports._error = function(err, res) { if (err) return err; else return new Error('got ' + res.status + ' response'); } -exports.error = error; diff --git a/lib/static.js b/lib/static.js index e8e9bdd..7136909 100644 --- a/lib/static.js +++ b/lib/static.js @@ -148,7 +148,7 @@ exports.destroyAll = function(fn){ .set(this._headers) .end(function(err, res){ if (err || res.error) - return fn(error(err,res), null, res); + return fn(self._error(err,res), null, res); fn(null, [], res); }); }; @@ -168,7 +168,7 @@ exports.all = function(fn){ .set(this._headers) .end(function(err, res){ if (err || res.error) - return fn(error(err,res), null, res); + return fn(self._error(err,res), null, res); var col = new Collection; for (var i = 0, len = res.body.length; i < len; ++i) { col.push(new self(res.body[i])); @@ -193,23 +193,21 @@ exports.get = function(id, fn){ .set(this._headers) .end(function(err, res){ if (err || res.error) - return fn(error(err,res), null, res); + return fn(self._error(err,res), null, res); var model = new self(res.body); fn(null, model, res); }); }; -/** - * Response error helper. - * - * @param {Response} er - * @return {Error} - * @api private - */ +exports.error = function(fn) { + if (fn) { + this._error = this.prototype._error = fn; + } -function error(err, res) { - if (err) return err; - else return new Error('got ' + res.status + ' response'); + return this; } -exports.error = error; +exports._error = function(err, res) { + if (err) return err; + else return new Error('got ' + res.status + ' response'); +} From 6ee13096f0722a961dbad93dfcd5e4ce25fb60e3 Mon Sep 17 00:00:00 2001 From: thrucker Date: Thu, 3 Jul 2014 08:09:48 +0200 Subject: [PATCH 08/17] restore accidentally deleted error method and fix name clash --- lib/proto.js | 48 ++++++++++++++++++++++++++++++++++++++---------- lib/static.js | 27 +++++++++++++++++++++------ 2 files changed, 59 insertions(+), 16 deletions(-) diff --git a/lib/proto.js b/lib/proto.js index 2d14530..9d1a8e3 100644 --- a/lib/proto.js +++ b/lib/proto.js @@ -25,6 +25,23 @@ Emitter(exports); */ exports.request = request; +/** + * Register an error `msg` on `attr`. + * + * @param {String} attr + * @param {String} msg + * @return {Object} self + * @api public + */ + +exports.error = function(attr, msg){ + this.errors.push({ + attr: attr, + message: msg + }); + return this; +}; + /** * Check if this model is new. * @@ -129,7 +146,7 @@ exports.destroy = function(fn){ .set(this.model._headers) .end(function(err, res){ if (err || res.error) - return fn(self._error(err,res), res); + return fn(self._onError(err,res), res); self.destroyed = true; self.model.emit('destroy', self, res); self.emit('destroy'); @@ -164,7 +181,7 @@ exports.save = function(fn){ .send(self) .end(function(err, res){ if (err || res.error) - return fn(self._error(err,res), res); + return fn(self._onError(err,res), res); if (res.body) self.primary(res.body[key]); self.dirty = {}; @@ -194,7 +211,7 @@ exports.update = function(fn){ .send(self) .end(function(err,res){ if (err || res.error) - return fn(self._error(err, res), res); + return fn(self._onError(err, res), res); self.dirty = {}; self.model.emit('save', self, res); self.emit('save'); @@ -275,20 +292,31 @@ exports.toJSON = function(){ }; /** - * Response error helper. + * Register a custom error handler for the model instance. * - * @param {Response} er - * @return {Error} - * @api private + * @param {Function} fn + * @return {Object} self + * @api public */ -exports.error = function(fn) { +exports.onError = function(fn) { if (fn) { - this._error = fn; + this._onError = fn; } + + return this; } -exports._error = function(err, res) { +/** + * Standard error handler. + * + * @param {Error} err + * @param {Response} res + * @return {Error} + * @api private + */ + +exports._onError = function(err, res) { if (err) return err; else return new Error('got ' + res.status + ' response'); } diff --git a/lib/static.js b/lib/static.js index 7136909..852f26b 100644 --- a/lib/static.js +++ b/lib/static.js @@ -148,7 +148,7 @@ exports.destroyAll = function(fn){ .set(this._headers) .end(function(err, res){ if (err || res.error) - return fn(self._error(err,res), null, res); + return fn(self._onError(err,res), null, res); fn(null, [], res); }); }; @@ -168,7 +168,7 @@ exports.all = function(fn){ .set(this._headers) .end(function(err, res){ if (err || res.error) - return fn(self._error(err,res), null, res); + return fn(self._onError(err,res), null, res); var col = new Collection; for (var i = 0, len = res.body.length; i < len; ++i) { col.push(new self(res.body[i])); @@ -193,21 +193,36 @@ exports.get = function(id, fn){ .set(this._headers) .end(function(err, res){ if (err || res.error) - return fn(self._error(err,res), null, res); + return fn(self._onError(err,res), null, res); var model = new self(res.body); fn(null, model, res); }); }; -exports.error = function(fn) { +/** + * Register a custom error handler for the model class and its instances. + * + * @param {Function} fn + * @return {Object} self + * @api public + */ +exports.onError = function(fn) { if (fn) { - this._error = this.prototype._error = fn; + this._onError = this.prototype._onError = fn; } return this; } -exports._error = function(err, res) { +/** + * Standard error handler. + * + * @param {Error} err + * @param {Response} res + * @return {Error} + * @api private + */ +exports._onError = function(err, res) { if (err) return err; else return new Error('got ' + res.status + ' response'); } From 541138536d8befc4a6cc798fa5c07c539f8a4dea Mon Sep 17 00:00:00 2001 From: thrucker Date: Thu, 3 Jul 2014 09:39:27 +0200 Subject: [PATCH 09/17] add test cases for onError methods --- test/model.js | 60 +++++++++++++++++++++++++++++++++++++++++++++++++ test/server.js | 16 +++++++++++++ test/statics.js | 49 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 125 insertions(+) diff --git a/test/model.js b/test/model.js index 3e36103..547e2cd 100644 --- a/test/model.js +++ b/test/model.js @@ -441,3 +441,63 @@ describe('Model#isValid()', function(){ assert(0 == user.errors.length); }) }) + +describe('Model#onError()', function() { + function getErrorHandler(static) { + return function(err, res) { + return {error: err, response: res, static: static}; + }; + } + + it('should be called on errors', function(done) { + var Car = model('Car') + .attr('id') + .attr('color'); + + var car = new Car({color: 'blue'}); + + car.onError(getErrorHandler(false)); + + car.save(function(err) { + assert(null == err.error); + assert(500 == err.response.status); + assert(false == err.static); + + done(); + }); + }); + + it('should overwrite the static error handler', function(done) { + var Car = model('Car') + .attr('id') + .attr('color') + .onError(getErrorHandler(true)); + + var car = new Car({color: 'red'}); + + car.onError(getErrorHandler(false)); + + car.save(function(err) { + assert(null == err.error); + assert(500 == err.response.status); + assert(false == err.static); + + done(); + }); + }); + + it('should use the default error handler if not called', function(done) { + var Car = model('Car') + .attr('id') + .attr('color'); + + var car = new Car({color: 'yellow'}); + + car.save(function(err) { + assert(err instanceof Error); + assert('got 500 response' == err.message); + + done(); + }); + }); +}); diff --git a/test/server.js b/test/server.js index f0abc86..48cc406 100644 --- a/test/server.js +++ b/test/server.js @@ -101,5 +101,21 @@ app.post('/users', function(req, res){ res.send({ id: id }); }); +app.get('/cars', function(req, res) { + setTimeout(function() { + res.send([]); + }, 500); +}); + +app.get('/cars/:id', function(req, res) { + setTimeout(function() { + res.send({id: req.params.id, color: 'silver'}); + }, 500); +}); + +app.post('/cars', function(req, res) { + res.send(500); +}); + app.listen(4000); console.log('test server listening on port 4000'); diff --git a/test/statics.js b/test/statics.js index d579f50..d441ba2 100644 --- a/test/statics.js +++ b/test/statics.js @@ -68,3 +68,52 @@ describe('Model.route(string)', function(){ assert('/api/u/edit' == User.url('edit')); }) }) + +describe('Model.onError()', function() { + var Car = model('Car') + .attr('id') + .attr('color') + .onError(function(err, res) { + return {error: err, response: res}; + }); + + var _request_get = Car.request.get; + + Car.request.get = function(url) { + var req = _request_get(url); + req.timeout(30); + return req; + }; + + it('should be called on errors', function(done) { + Car.all(function(err) { + assert(err.error instanceof Error); + assert(-1 != err.error.message.indexOf('timeout')); + + done(); + }); + }); + + it('should be inherited by model instances', function(done) { + var car = new Car({color: 'silver'}); + + car.save(function(err) { + assert(500 == err.response.status); + + done(); + }); + }); + + it('should use the default error handler if not called', function(done) { + var Car = model('Car') + .attr('id') + .attr('color'); + + Car.all(function(err) { + assert(err instanceof Error); + assert(-1 != err.message.indexOf('timeout')); + + done(); + }); + }); +}); From 9100906d96839751c738419d77b1f977e7ab9218 Mon Sep 17 00:00:00 2001 From: thrucker Date: Thu, 3 Jul 2014 09:55:29 +0200 Subject: [PATCH 10/17] add documentation for onError methods --- Readme.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Readme.md b/Readme.md index c0106c6..7e3fe39 100644 --- a/Readme.md +++ b/Readme.md @@ -207,6 +207,25 @@ user.save(function(err){ Destroy and invoke optional `fn(err)`. Emits "destroy" when successfully deleted. + +### .onError(fn) + + Register a custom error handler for the model class and/or model instances. + If called on the Model class, the error handler is used for static methods as well as the methods of the model + instances. If called on a model instance, the error handler is only used for methods of this instance. + +```js +var handler1 = function(err, res) {...}; +var handler2 = function(err, res) {...}; + +var Car = model('Car') + .onError(handler1); + +var car1 = new Car(); // uses handler1 + +var car2 = new Car(); +car2.onError(handler2); // uses handler2 +``` ## Links From 5b4234b2fe7d3bfb9089c13b83cdf90832f05c2a Mon Sep 17 00:00:00 2001 From: fschoenfelder Date: Thu, 3 Jul 2014 11:10:08 +0200 Subject: [PATCH 11/17] clean up test code for timeout errors --- test/statics.js | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/test/statics.js b/test/statics.js index d441ba2..e1e81bc 100644 --- a/test/statics.js +++ b/test/statics.js @@ -79,14 +79,23 @@ describe('Model.onError()', function() { var _request_get = Car.request.get; - Car.request.get = function(url) { - var req = _request_get(url); - req.timeout(30); - return req; - }; + before( function(done) { + Car.request.get = function(url, data, fn) { + var req = _request_get(url, data, fn); + req.timeout(1); + return req; + }; + done(); + }); + + after( function(done) { + Car.request.get = _request_get; + done(); + }); + it('should be called on errors', function(done) { - Car.all(function(err) { + Car.all(function(err, res) { assert(err.error instanceof Error); assert(-1 != err.error.message.indexOf('timeout')); From f2fcf46ad6d22814578cdc332bcb7e29a6bad1ee Mon Sep 17 00:00:00 2001 From: fschoenfelder Date: Thu, 3 Jul 2014 11:33:39 +0200 Subject: [PATCH 12/17] change 'onError' 'toError' --- lib/proto.js | 12 ++++++------ lib/static.js | 12 ++++++------ test/model.js | 8 ++++---- test/statics.js | 6 ++---- 4 files changed, 18 insertions(+), 20 deletions(-) diff --git a/lib/proto.js b/lib/proto.js index 9d1a8e3..2a5d6ba 100644 --- a/lib/proto.js +++ b/lib/proto.js @@ -146,7 +146,7 @@ exports.destroy = function(fn){ .set(this.model._headers) .end(function(err, res){ if (err || res.error) - return fn(self._onError(err,res), res); + return fn(self._toError(err,res), res); self.destroyed = true; self.model.emit('destroy', self, res); self.emit('destroy'); @@ -181,7 +181,7 @@ exports.save = function(fn){ .send(self) .end(function(err, res){ if (err || res.error) - return fn(self._onError(err,res), res); + return fn(self._toError(err,res), res); if (res.body) self.primary(res.body[key]); self.dirty = {}; @@ -211,7 +211,7 @@ exports.update = function(fn){ .send(self) .end(function(err,res){ if (err || res.error) - return fn(self._onError(err, res), res); + return fn(self._toError(err, res), res); self.dirty = {}; self.model.emit('save', self, res); self.emit('save'); @@ -299,9 +299,9 @@ exports.toJSON = function(){ * @api public */ -exports.onError = function(fn) { +exports.toError = function(fn) { if (fn) { - this._onError = fn; + this._toError = fn; } return this; @@ -316,7 +316,7 @@ exports.onError = function(fn) { * @api private */ -exports._onError = function(err, res) { +exports._toError = function(err, res) { if (err) return err; else return new Error('got ' + res.status + ' response'); } diff --git a/lib/static.js b/lib/static.js index 852f26b..27b504c 100644 --- a/lib/static.js +++ b/lib/static.js @@ -148,7 +148,7 @@ exports.destroyAll = function(fn){ .set(this._headers) .end(function(err, res){ if (err || res.error) - return fn(self._onError(err,res), null, res); + return fn(self._toError(err,res), null, res); fn(null, [], res); }); }; @@ -168,7 +168,7 @@ exports.all = function(fn){ .set(this._headers) .end(function(err, res){ if (err || res.error) - return fn(self._onError(err,res), null, res); + return fn(self._toError(err,res), null, res); var col = new Collection; for (var i = 0, len = res.body.length; i < len; ++i) { col.push(new self(res.body[i])); @@ -193,7 +193,7 @@ exports.get = function(id, fn){ .set(this._headers) .end(function(err, res){ if (err || res.error) - return fn(self._onError(err,res), null, res); + return fn(self._toError(err,res), null, res); var model = new self(res.body); fn(null, model, res); }); @@ -206,9 +206,9 @@ exports.get = function(id, fn){ * @return {Object} self * @api public */ -exports.onError = function(fn) { +exports.toError = function(fn) { if (fn) { - this._onError = this.prototype._onError = fn; + this._toError = this.prototype._toError = fn; } return this; @@ -222,7 +222,7 @@ exports.onError = function(fn) { * @return {Error} * @api private */ -exports._onError = function(err, res) { +exports._toError = function(err, res) { if (err) return err; else return new Error('got ' + res.status + ' response'); } diff --git a/test/model.js b/test/model.js index 547e2cd..f088afd 100644 --- a/test/model.js +++ b/test/model.js @@ -442,7 +442,7 @@ describe('Model#isValid()', function(){ }) }) -describe('Model#onError()', function() { +describe('Model#toError()', function() { function getErrorHandler(static) { return function(err, res) { return {error: err, response: res, static: static}; @@ -456,7 +456,7 @@ describe('Model#onError()', function() { var car = new Car({color: 'blue'}); - car.onError(getErrorHandler(false)); + car.toError(getErrorHandler(false)); car.save(function(err) { assert(null == err.error); @@ -471,11 +471,11 @@ describe('Model#onError()', function() { var Car = model('Car') .attr('id') .attr('color') - .onError(getErrorHandler(true)); + .toError(getErrorHandler(true)); var car = new Car({color: 'red'}); - car.onError(getErrorHandler(false)); + car.toError(getErrorHandler(false)); car.save(function(err) { assert(null == err.error); diff --git a/test/statics.js b/test/statics.js index e1e81bc..1c3a475 100644 --- a/test/statics.js +++ b/test/statics.js @@ -69,11 +69,11 @@ describe('Model.route(string)', function(){ }) }) -describe('Model.onError()', function() { +describe('Model.toError()', function() { var Car = model('Car') .attr('id') .attr('color') - .onError(function(err, res) { + .toError(function(err, res) { return {error: err, response: res}; }); @@ -98,7 +98,6 @@ describe('Model.onError()', function() { Car.all(function(err, res) { assert(err.error instanceof Error); assert(-1 != err.error.message.indexOf('timeout')); - done(); }); }); @@ -108,7 +107,6 @@ describe('Model.onError()', function() { car.save(function(err) { assert(500 == err.response.status); - done(); }); }); From 9568ab1e3f167ffc0d2ac7aec8196485b814205c Mon Sep 17 00:00:00 2001 From: fschoenfelder Date: Thu, 3 Jul 2014 13:20:31 +0200 Subject: [PATCH 13/17] change test comment --- test/statics.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/statics.js b/test/statics.js index 1c3a475..8245090 100644 --- a/test/statics.js +++ b/test/statics.js @@ -94,7 +94,7 @@ describe('Model.toError()', function() { }); - it('should be called on errors', function(done) { + it('should be called on errors (e.g. request timeout)', function(done) { Car.all(function(err, res) { assert(err.error instanceof Error); assert(-1 != err.error.message.indexOf('timeout')); From 8428cba6f7573ba5ae52f2f7c198e5079afc3b92 Mon Sep 17 00:00:00 2001 From: Anton Wilhelm Date: Mon, 15 Jun 2015 00:20:32 +0200 Subject: [PATCH 14/17] provide browserify / jspm support --- package.json | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 6cacab3..adae390 100644 --- a/package.json +++ b/package.json @@ -5,10 +5,17 @@ "main": "lib/index.js", "dependencies": { "component-emitter": "~1.1.3", - "collection": "git://github.com/component/collection#0.0.2", + "component-collection": "~0.0.2", "component-each": "~0.2.5", "superagent": "~0.21.0" }, + "browser": { + "map": { + "emitter": "component-emitter", + "collection": "component-collection", + "each": "component-each" + } + }, "devDependencies": { "express": "3.0.0" } From e632f199532a136749384ce043974269636c47b5 Mon Sep 17 00:00:00 2001 From: Anton Wilhelm Date: Sun, 21 Jun 2015 19:32:01 +0200 Subject: [PATCH 15/17] fix browser aliases --- package.json | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index adae390..3f67577 100644 --- a/package.json +++ b/package.json @@ -10,11 +10,9 @@ "superagent": "~0.21.0" }, "browser": { - "map": { - "emitter": "component-emitter", - "collection": "component-collection", - "each": "component-each" - } + "emitter": "component-emitter", + "collection": "component-collection", + "each": "component-each" }, "devDependencies": { "express": "3.0.0" From ccb8c1c17dc4e5ee4a8012d5117d99c1596da80d Mon Sep 17 00:00:00 2001 From: Anton Wilhelm Date: Sun, 21 Jun 2015 19:33:31 +0200 Subject: [PATCH 16/17] Update static.js --- lib/static.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/static.js b/lib/static.js index 27b504c..9d52ba9 100644 --- a/lib/static.js +++ b/lib/static.js @@ -2,6 +2,13 @@ * Module dependencies. */ +try { + var Emitter = require('emitter'); + var each = require('each'); +} catch (e) { + var Emitter = require('component-emitter'); + var each = require('component-each'); +} var Collection = require('collection'); var request = require('superagent'); var noop = function(){}; From 12c8193639d2cb5c9267ebb7ee07dbb92d2aa3f4 Mon Sep 17 00:00:00 2001 From: Anton Wilhelm Date: Sun, 21 Jun 2015 19:34:37 +0200 Subject: [PATCH 17/17] use try catch, since it can be used in nodejs --- lib/static.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/lib/static.js b/lib/static.js index 9d52ba9..7b6759b 100644 --- a/lib/static.js +++ b/lib/static.js @@ -3,13 +3,10 @@ */ try { - var Emitter = require('emitter'); - var each = require('each'); + var Collection = require('collection'); } catch (e) { - var Emitter = require('component-emitter'); - var each = require('component-each'); + var Collection = require('component-collection'); } -var Collection = require('collection'); var request = require('superagent'); var noop = function(){};