From 6f59c0cda1b49ccbef5810aef35592f8b76662e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Soko=C5=82owski?= Date: Thu, 13 Dec 2018 10:34:38 +0100 Subject: [PATCH] improve error handling with koa-json-error MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jakub SokoĊ‚owski --- package.json | 1 + src/app.js | 6 ++++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 29d9bbd..0e3aa69 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "koa-bodyparser": "^4.2.1", "koa-joi-router": "^5.1.0", "koa-json": "^2.0.2", + "koa-json-error": "^3.1.2", "koa-logger": "^3.2.0", "lokijs": "^1.5.5", "nunjucks": "^3.1.4" diff --git a/src/app.js b/src/app.js index 6e486c9..9469105 100644 --- a/src/app.js +++ b/src/app.js @@ -1,5 +1,6 @@ import Koa from 'koa' import JSON from 'koa-json' +import JsonError from 'koa-json-error' import JoiRouter from 'koa-joi-router' import BodyParser from 'koa-bodyparser' @@ -7,9 +8,10 @@ const App = (ghc) => { const app = new Koa() const router = new JoiRouter() - app.use(router.middleware()) + app.use(JSON({pretty: true})) + .use(JsonError()) + .use(router.middleware()) .use(BodyParser({onerror:console.error})) - .use(JSON({pretty: true})) app.on('error', (err, ctx) => { console.error('server error', err, ctx)