From: Justin Wind Date: Sat, 26 Aug 2023 19:39:54 +0000 (-0700) Subject: minor refactor of router to use explicit exceptions X-Git-Tag: v2.0.0^2~4 X-Git-Url: https://git.squeep.com/?a=commitdiff_plain;h=91ca5d0b0e93afeb5c68a48fef461bbcc095fcbe;p=squeep-api-dingus minor refactor of router to use explicit exceptions --- diff --git a/lib/dingus.js b/lib/dingus.js index 5b4643c..484debc 100644 --- a/lib/dingus.js +++ b/lib/dingus.js @@ -13,7 +13,7 @@ const querystring = require('querystring'); const common = require('./common'); const ContentNegotiation = require('./content-negotiation'); const Enum = require('./enum'); -const { DingusError, ResponseError } = require('./errors'); +const { DingusError, ResponseError, RouterNoMethodError, RouterNoPathError } = require('./errors'); const { extensionToMime } = require('./mime-helper'); const Router = require('./router'); const Template = require('./template'); @@ -268,18 +268,13 @@ class Dingus { try { ({ handler, handlerArgs } = this.router.lookup(req.method, pathPart, ctx)); } catch (e) { - if (e instanceof DingusError) { - switch (e.message) { - case 'NoPath': - handler = this.handlerNotFound.bind(this); - break; - case 'NoMethod': - handler = this.handlerMethodNotAllowed.bind(this); - break; - default: - this.logger.error(_scope, 'unknown dingus error', { error: e }); - handler = this.handlerInternalServerError.bind(this); - } + if (e instanceof RouterNoPathError) { + handler = this.handlerNotFound.bind(this); + } else if (e instanceof RouterNoMethodError) { + handler = this.handlerMethodNotAllowed.bind(this); + } else if (e instanceof DingusError) { + this.logger.error(_scope, 'unknown dingus error', { error: e }); + handler = this.handlerInternalServerError.bind(this); } else if (e instanceof URIError) { handler = this.handlerBadRequest.bind(this); } else { diff --git a/lib/errors.js b/lib/errors.js index ed5b151..942ecc0 100644 --- a/lib/errors.js +++ b/lib/errors.js @@ -22,7 +22,23 @@ class ResponseError extends DingusError { } } +class RouterError extends DingusError { + constructor(...args) { + super(...args); + delete this.stack; + } +} + +class RouterNoPathError extends RouterError { +} + +class RouterNoMethodError extends RouterError { +} + module.exports = { DingusError, ResponseError, + RouterError, + RouterNoPathError, + RouterNoMethodError, }; diff --git a/lib/router/index.js b/lib/router/index.js index 1585065..fd19bdf 100644 --- a/lib/router/index.js +++ b/lib/router/index.js @@ -7,7 +7,7 @@ const { METHODS: httpMethods } = require('http'); const common = require('../common'); -const { DingusError } = require('../errors'); +const { DingusError, RouterNoPathError, RouterNoMethodError } = require('../errors'); const PathParameter = require('./path-parameter'); // Internal identifiers for route entries. @@ -266,10 +266,10 @@ class Router { if ('*' in matchedPath[kPathMethods]) { return matchedPath[kPathMethods]['*']; } - throw new DingusError('NoMethod'); + throw new RouterNoMethodError(); } ctx.unmatchedPath = pathParts; - throw new DingusError('NoPath'); + throw new RouterNoPathError(); } diff --git a/test/lib/router.js b/test/lib/router.js index 8a9d1e2..bcb45f2 100644 --- a/test/lib/router.js +++ b/test/lib/router.js @@ -6,7 +6,7 @@ const assert = require('assert'); const sinon = require('sinon'); // eslint-disable-line node/no-unpublished-require const Router = require('../../lib/router'); const PathParameter = require('../../lib/router/path-parameter'); -const { DingusError } = require('../../lib/errors'); +const { DingusError, RouterNoPathError, RouterNoMethodError } = require('../../lib/errors'); const noExpectedException = 'did not get expected exception'; @@ -311,8 +311,7 @@ describe('Router', function () { router.lookup(method, path, ctx); assert.fail(noExpectedException); } catch (e) { - assert(e instanceof DingusError); - assert.strictEqual(e.message, 'NoPath'); + assert(e instanceof RouterNoPathError); } }); it('finds handler', function () { @@ -353,8 +352,7 @@ describe('Router', function () { router.lookup(method, path, ctx); assert.fail(noExpectedException); } catch (e) { - assert(e instanceof DingusError); - assert.strictEqual(e.message, 'NoMethod'); + assert(e instanceof RouterNoMethodError); } }); it('does not lookup non-existent path', async function () { @@ -365,8 +363,7 @@ describe('Router', function () { router.lookup(method, path, ctx); assert.fail(noExpectedException); } catch (e) { - assert(e instanceof DingusError); - assert.strictEqual(e.message, 'NoPath'); + assert(e instanceof RouterNoPathError); } });