X-Git-Url: https://git.squeep.com/?a=blobdiff_plain;ds=sidebyside;f=lib%2Fdingus.js;h=484debced43f0732a71ca0967d964b6475256a29;hb=91ca5d0b0e93afeb5c68a48fef461bbcc095fcbe;hp=d8eed919a6fd992b463f2fc154edc034ff7dfdb2;hpb=cea77553ae5746713defff5528a475c702797f37;p=squeep-api-dingus diff --git a/lib/dingus.js b/lib/dingus.js index d8eed91..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'); @@ -44,7 +44,7 @@ class Dingus { * @param {Boolean} options.trustProxy trust some header data to be provided by proxy * @param {Object} options.querystring alternate qs parser to use */ - constructor(logger = common.nullLogger, options = {}) { + constructor(logger = console, options = {}) { common.setOptions(this, defaultOptions, options); this.router = new Router(options); @@ -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 { @@ -313,16 +308,11 @@ class Dingus { * Parse rawBody as contentType into ctx.parsedBody. * @param {string} contentType * @param {object} ctx - * @param {string|buffer} + * @param {string|buffer} rawBody */ parseBody(contentType, ctx, rawBody) { const _scope = _fileScope('parseBody'); - if (!rawBody) { - // 1.2.4 and earlier expected rawBody on context - rawBody = ctx.rawBody; - } - switch (contentType) { case Enum.ContentType.ApplicationForm: ctx.parsedBody = this.querystring.parse(rawBody);