From 5583948a89a50e4a90e876f1f4ec510c5a480eac Mon Sep 17 00:00:00 2001 From: Gloria Loi Date: Tue, 21 Sep 2021 16:43:50 +0300 Subject: [PATCH 1/2] Make invalid schema message more readable --- src/config/koa.js | 4 +++- src/middlewares/attachThrowError.js | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/config/koa.js b/src/config/koa.js index 29947cd..8992184 100644 --- a/src/config/koa.js +++ b/src/config/koa.js @@ -16,10 +16,12 @@ const routeErrorHandler = async (ctx, next) => { const status = error.status || 500; const errors = error.errors || error.message; + const errorDetails = errors.match('details":\\[(.*)\\]}'); ctx.status = status; + ctx.body = process.env.APP_ENV === 'production' ? { errors: error.errors || { _global: ['Something went wrong.'] } } - : { errors: error.errors || { _global: [error.message] } }; + : { errors: error.errors || { _global: errorDetails ? [JSON.parse(errorDetails[1])?.message] : [error.message] } }; logger.error(errors); } diff --git a/src/middlewares/attachThrowError.js b/src/middlewares/attachThrowError.js index 923f991..807a77b 100644 --- a/src/middlewares/attachThrowError.js +++ b/src/middlewares/attachThrowError.js @@ -6,7 +6,7 @@ const attachThrowError = async (ctx, next) => { }; ctx.assertError = (condition, errors = ['Something went wrong.'], status = 400) => { - ctx.assert(condition, status, errors); + ctx.assert(!condition, status, errors); return null; }; From ccdfc85cca2f77a4f242e997ee2cf0602599a145 Mon Sep 17 00:00:00 2001 From: Gloria Loi Date: Tue, 21 Sep 2021 18:17:30 +0300 Subject: [PATCH 2/2] Add message to logger --- src/config/koa.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/config/koa.js b/src/config/koa.js index 8992184..2bbae20 100644 --- a/src/config/koa.js +++ b/src/config/koa.js @@ -23,7 +23,7 @@ const routeErrorHandler = async (ctx, next) => { ? { errors: error.errors || { _global: ['Something went wrong.'] } } : { errors: error.errors || { _global: errorDetails ? [JSON.parse(errorDetails[1])?.message] : [error.message] } }; - logger.error(errors); + logger.error(errorDetails[1] || errors); } };