Correctly handle not found errors in partial routes

Take care of obvious case. A router with a route to '/foo/bar' but not
for '/foo', should give a not found error.
master
Buddy Sandidge 9 years ago
parent 9da1eabbf2
commit 79fb5395ab

@ -0,0 +1,14 @@
var util = require('util')
function NotFound(message, options) {
Error.call(this)
options = options || {}
this.message = message || ''
this.location = options.location || ''
}
util.inherits(NotFound, Error)
module.exports = {
NotFound: NotFound
}

@ -1,4 +1,5 @@
'use strict' 'use strict'
var NotFoundError = require('./error').NotFound
var utils = require('./utils') var utils = require('./utils')
var assign = utils.assign var assign = utils.assign
@ -10,8 +11,8 @@ var noop = utils.noop
function RouteNode (options) { function RouteNode (options) {
options = options || {} options = options || {}
this._children = Object.create(null) this._children = Object.create(null)
this._regExs = Object.create(null)
this._funcs = Object.create(null) this._funcs = Object.create(null)
this._regExs = Object.create(null)
} }
assign(RouteNode.prototype, { assign(RouteNode.prototype, {
@ -71,7 +72,7 @@ assign(RouteNode.prototype, {
return results.node.get(parts, args.concat(results.args), done) return results.node.get(parts, args.concat(results.args), done)
} }
return done(new Error('not found')) return done(new NotFoundError('not found', {location: parts}))
}, },
_getRegexChild: function _getRegexChild(part) { _getRegexChild: function _getRegexChild(part) {

@ -1,10 +1,12 @@
'use strict' 'use strict'
var url = require('url') var url = require('url')
var NotFoundError = require('./error').NotFound
var RouteNode = require('./route-node') var RouteNode = require('./route-node')
var utils = require('./utils') var utils = require('./utils')
var assign = utils.assign var assign = utils.assign
var isArray = utils.isArray var isArray = utils.isArray
var isFunction = utils.isFunction
var isString = utils.isString var isString = utils.isString
var noop = utils.noop var noop = utils.noop
@ -25,7 +27,11 @@ assign(Router.prototype, {
if (err) { if (err) {
return done(err) return done(err)
} else { } else {
if (isFunction(func)) {
return done(null, func.apply(context, args)) return done(null, func.apply(context, args))
} else {
return done(new NotFoundError('not found', {location: path}))
}
} }
}) })
}, },
@ -53,7 +59,7 @@ assign(Router.prototype, {
}) })
Router.isNotFound = function isNotFound(err) { Router.isNotFound = function isNotFound(err) {
return err instanceof Error && /not found/.exec(err.message) !== null return err instanceof NotFoundError
} }
module.exports = Router module.exports = Router

@ -1,5 +1,6 @@
var expect = require('expect') var expect = require('expect')
var Router = require('../lib/router') var Router = require('../lib/router')
var NotFoundError = require('../lib/error').NotFound
describe('Router', function () { describe('Router', function () {
it('→ exists', function () { it('→ exists', function () {
@ -26,6 +27,15 @@ describe('Router', function () {
}) })
}) })
it('→ partial route is a not found error', function (done) {
var router = new Router()
router.add(['/user', /^(\d+)$/], function (number) { return number })
router.route('/user/', function (err) {
expect(err).toBeA(NotFoundError)
done()
})
})
it('→ get error in callback for missing handler', function (done) { it('→ get error in callback for missing handler', function (done) {
var router = new Router() var router = new Router()
router.route('/some/fake/path', function (err, handlerResult) { router.route('/some/fake/path', function (err, handlerResult) {

Loading…
Cancel
Save