From 58c390fc616b822a64fbc827656b0b8207fffa60 Mon Sep 17 00:00:00 2001 From: Timo Hocker Date: Thu, 9 Jan 2020 08:33:28 +0100 Subject: [PATCH] add rethrow option --- index.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 6e63e28..7d8e910 100644 --- a/index.js +++ b/index.js @@ -8,6 +8,7 @@ const path = require ('path'); * @property {any} [opts] object to pass to the handlers * @property {string} [subdir] subdirectory for all requests * @property {boolean} [verbose] enable verbose logging + * @property {boolean} [rethrow] rethrow errors (default: true) */ /** @@ -20,9 +21,9 @@ const path = require ('path'); module.exports = function main ( app, modulefolder, - options = { opts: null, subdir: '', verbose: false } + options = { opts: null, subdir: '', verbose: false, rethrow: true } ) { - const { opts, subdir, verbose } = options; + const { opts, subdir, verbose, rethrow } = options; for (const f of fs.readdirSync (modulefolder)) { const regex = /(?.*?)-(?.*?)\.js/u; @@ -42,7 +43,15 @@ module.exports = function main ( const handler = require (path.join (process.cwd (), modulefolder, f)); const requestor_handler = (req, res, next) => { - handler (req, res, next, opts); + try { + handler (req, res, next, opts); + } + catch (e) { + if (rethrow) + throw e; + else + console.error (e); + } }; if (verbose)