diff --git a/src/route.ts b/src/route.ts index 104080d5..0d06132c 100644 --- a/src/route.ts +++ b/src/route.ts @@ -77,6 +77,19 @@ interface Route { handler: Handler; } +const youchErrors = (fun: any): any => async (req: IM, res: SR): Promise => { + try { + return await fun(req, res); + } catch (err) { + if (!isProd) { + const youch = new Youch(err, req); + const json = await youch.toJSON(); + console.log(forTerminal(json)); // eslint-disable-line + } + throw err; + } +}; + export default (route: Route): Handler => { const fn = async (Req: IM, Res: SR): AP => { let exec = async (req: IM, res: SR): AP => { @@ -95,6 +108,12 @@ export default (route: Route): Handler => { const plugins = route.plugins || []; + plugins.unshift(handleErrors); + + if (!isProd) { + plugins.unshift(youchErrors); + } + if (fn.log !== false) { plugins.unshift(logger); fn.log = false; @@ -104,20 +123,7 @@ export default (route: Route): Handler => { exec = plugins.reverse().reduce((acc, val): any => val(acc), exec); } - const youchErrors = (fun: any): any => async (req: IM, res: SR): Promise => { - try { - return await fun(req, res); - } catch (err) { - if (!isProd) { - const youch = new Youch(err, req); - const json = await youch.toJSON(); - console.log(forTerminal(json)); // eslint-disable-line - } - throw err; - } - }; - - return run(Req, Res, handleErrors(youchErrors(exec))); + return run(Req, Res, exec); }; Object.keys(route).forEach((key): void => { diff --git a/tsconfig.json b/tsconfig.json index 8f1c1be3..3d7591ff 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,7 +1,7 @@ { "compilerOptions": { /* Basic Options */ - "target": "ES2015", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */ + "target": "ES2017", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */ "module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */ // "lib": [], /* Specify library files to be included in the compilation. */ // "allowJs": true, /* Allow javascript files to be compiled. */