From d7654b74502465587da1e490c09e486fbf43f6db Mon Sep 17 00:00:00 2001 From: jonschlinkert Date: Sat, 21 May 2016 15:06:17 -0400 Subject: [PATCH] fix errors in routes --- lib/plugins/routes.js | 65 ++++++++++++++++++++++++------------------- test/app.route.js | 14 +++++----- 2 files changed, 44 insertions(+), 35 deletions(-) diff --git a/lib/plugins/routes.js b/lib/plugins/routes.js index d445a0b..0286359 100644 --- a/lib/plugins/routes.js +++ b/lib/plugins/routes.js @@ -24,6 +24,7 @@ module.exports = function(proto) { methods: utils.methods })); } + if (typeof methods !== 'undefined') { this.router.method(methods); } @@ -56,15 +57,15 @@ module.exports = function(proto) { view.options.handled = []; } - var done = this.handleError(method, view, next); + var cb = this.handleError(method, view, next); view.options.method = method; view.options.handled.push(method); this.emit(method, view); // if inside a collection or the collection is not specified on view.options // just handle the route and return - if (this.isCollection || !view.options.collection) { - this.router.handle(view, done); + if (!this.isTemplates || this.isCollection || !view.options.collection) { + this.router.handle(view, cb); return; } @@ -72,8 +73,8 @@ module.exports = function(proto) { var collection = this[view.options.collection]; this.router.handle(view, function(err) { - if (err) return done(err); - collection.handle(method, view, done); + if (err) return cb(err); + collection.handle(method, view, cb); }); }; @@ -117,21 +118,31 @@ module.exports = function(proto) { * Handle middleware errors. */ - proto.handleError = function(method, view, cb) { - if (typeof cb !== 'function') { - cb = utils.identity; - } + proto.handleError = function(method, view, next) { var app = this; + + if (typeof next !== 'function') { + next = utils.identity; + } + return function(err) { if (err) { - if (err._handled) return cb(); - utils.define(err, '_handled', true); err.source = err.stack.split('\n')[1].trim(); err.reason = app._name + '#handle("' + method + '"): ' + view.path; - app.emit('error', err); - return cb(err); + + if (app.hasListeners('error')) { + app.emit('error', err); + } + + if (typeof next !== 'function') throw err; + next(err); + return; } - cb(null, view); + + if (typeof next !== 'function') { + throw new TypeError('expected a callback function'); + } + next(null, view); }; }; @@ -181,6 +192,7 @@ module.exports = function(proto) { */ proto.all = function(path/*, callback*/) { + this.lazyRouter(); var route = this.route(path); route.all.apply(route, [].slice.call(arguments, 1)); return this; @@ -223,6 +235,7 @@ module.exports = function(proto) { proto.handler = function(methods) { this.handlers(methods); + return this; }; /** @@ -231,25 +244,21 @@ module.exports = function(proto) { proto.handlers = function(methods) { this.lazyRouter(methods); + mixinHandlers(methods); + return this; + }; + // Mix router handler methods onto the instance + mixinHandlers(utils.methods); + function mixinHandlers(methods) { utils.arrayify(methods).forEach(function(method) { - this.define(method, function(path) { + utils.define(proto, method, function(path) { var route = this.route(path); var args = [].slice.call(arguments, 1); route[method].apply(route, args); return this; - }.bind(this)); - }.bind(this)); - }; - - // Add router methods to Templates - utils.methods.forEach(function(method) { - proto[method] = function(path) { - var route = this.route(path); - var args = [].slice.call(arguments, 1); - route[method].apply(route, args); - return this; - }; - }); + }); + }); + } }; diff --git a/test/app.route.js b/test/app.route.js index 9b7689f..bc4ebaf 100644 --- a/test/app.route.js +++ b/test/app.route.js @@ -18,7 +18,7 @@ describe('app.route', function() { app.create('posts'); app.on('all', function(msg) { - assert(msg === 'cb'); + assert.equal(msg, 'cb'); cb(); }); @@ -36,17 +36,17 @@ describe('app.route', function() { app.create('posts'); app.on('onLoad', function(view) { - assert(view.path === 'blog/foo.js'); + assert.equal(view.path, 'blog/foo.js'); cb(); }); app.param('title', function(view, next, title) { - assert(title === 'foo.js'); + assert.equal(title, 'foo.js'); next(); }); app.onLoad('blog/:title', function(view, next) { - assert(view.path === 'blog/foo.js'); + assert.equal(view.path, 'blog/foo.js'); next(); }); @@ -58,18 +58,18 @@ describe('app.route', function() { app.create('posts'); app.on('error', function(err) { - assert(err.message === 'false == true'); + assert.equal(err.message, "'foo.js' == 'fo.js'"); cb(); }); // wrong... app.param('title', function(view, next, title) { - assert(title === 'fo.js'); + assert.equal(title, 'fo.js'); next(); }); app.onLoad('/blog/:title', function(view, next) { - assert(view.path === '/blog/foo.js'); + assert.equal(view.path, '/blog/foo.js'); next(); });