From 989ae620b3a46afd9a9d7b73294c1834f3e04204 Mon Sep 17 00:00:00 2001 From: Jay Date: Fri, 17 Aug 2018 23:08:21 +0800 Subject: [PATCH] fix api response --- route/api/index.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/route/api/index.js b/route/api/index.js index 4d9166d..9c2a0fc 100644 --- a/route/api/index.js +++ b/route/api/index.js @@ -16,12 +16,24 @@ r.use(async (c, n) => { c.obj = {} c.db = await DB.connect() c.chkBody = chkObject.bind(c.request) + + let obj = null try { await n() } catch (err) { console.log(err) - c.obj = resObject(err instanceof APIError ? err.resKey : 'InternalError', err.apiMsg || null, err.msgCode || null) + obj = resObject(err instanceof APIError ? err.resKey : 'InternalError', err.apiMsg || null, err.msgCode || null) } + + if (Object.keys(c.obj).length > 0 && 'status' in c.obj && 'obj' in c.obj) { + c.status = c.obj.status + c.body = c.obj.obj + } + if (obj !== null) { + c.status = obj.status + c.body = obj.obj + } + c.db.release() })