diff --git a/src/config/koa.js b/src/config/koa.js index 29947cd..2bbae20 100644 --- a/src/config/koa.js +++ b/src/config/koa.js @@ -16,12 +16,14 @@ 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); + logger.error(errorDetails[1] || 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; };