Skip to content

Commit

Permalink
fix errors in routes
Browse files Browse the repository at this point in the history
  • Loading branch information
jonschlinkert committed May 21, 2016
1 parent b48cb77 commit d7654b7
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 35 deletions.
65 changes: 37 additions & 28 deletions lib/plugins/routes.js
Expand Up @@ -24,6 +24,7 @@ module.exports = function(proto) {
methods: utils.methods
}));
}

if (typeof methods !== 'undefined') {
this.router.method(methods);
}
Expand Down Expand Up @@ -56,24 +57,24 @@ 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;
}

// handle the app routes first, then handle the collection routes
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);
});
};

Expand Down Expand Up @@ -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);
};
};

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -223,6 +235,7 @@ module.exports = function(proto) {

proto.handler = function(methods) {
this.handlers(methods);
return this;
};

/**
Expand All @@ -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;
};
});
});
});
}
};

14 changes: 7 additions & 7 deletions test/app.route.js
Expand Up @@ -18,7 +18,7 @@ describe('app.route', function() {
app.create('posts');

app.on('all', function(msg) {
assert(msg === 'cb');
assert.equal(msg, 'cb');
cb();
});

Expand All @@ -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();
});

Expand All @@ -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();
});

Expand Down

0 comments on commit d7654b7

Please sign in to comment.