From 62e6469ec72f009b5169182710e807fcc8b451be Mon Sep 17 00:00:00 2001 From: Buddy Sandidge Date: Sun, 7 Feb 2016 22:01:02 -0800 Subject: [PATCH] Add static isNotFound error checker to Router class If used, it will allow the error message to change without inspecting the error.message --- lib/router.js | 5 ++++- test/router.js | 12 ++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/router.js b/lib/router.js index 14c11fa..e186ab5 100644 --- a/lib/router.js +++ b/lib/router.js @@ -50,7 +50,10 @@ assign(Router.prototype, { return memo.concat(item) } } - }) +Router.isNotFound = function isNotFound(err) { + return err instanceof Error && /not found/.exec(err.message) !== null +} + module.exports = Router diff --git a/test/router.js b/test/router.js index 7600c2c..8baab72 100644 --- a/test/router.js +++ b/test/router.js @@ -96,4 +96,16 @@ describe('Router', function () { done() }) }) + + it('→ check not found error', function (done) { + var router = new Router() + router.route('/not/real/id', function (err) { + expect(Router.isNotFound(err)).toBe(true) + done() + }) + }) + + it('→ check not found error on not real error', function () { + expect(Router.isNotFound(new Error('Not the missing error'))).toBe(false) + }) })