From 02b47f963c00647407ceda721f0313d59e7bdbd4 Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 26 Oct 2018 14:38:34 +0900 Subject: [PATCH] API: Better error response --- src/server/api/call.ts | 10 +++++++++- src/server/api/get-params.ts | 4 +++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/server/api/call.ts b/src/server/api/call.ts index 20cb1ab7d5..b87707c348 100644 --- a/src/server/api/call.ts +++ b/src/server/api/call.ts @@ -62,7 +62,15 @@ export default (endpoint: string, user: IUser, app: IApp, data: any, file?: any) console.warn(`SLOW API CALL DETECTED: ${ep.name} (${time}ms)`); } } catch (e) { - rej(e.message); + if (e.name == 'INVALID_PARAM') { + rej({ + code: e.name, + param: e.param, + reason: e.message + }); + } else { + rej(e); + } return; } diff --git a/src/server/api/get-params.ts b/src/server/api/get-params.ts index e495e3ef3e..878f223cc3 100644 --- a/src/server/api/get-params.ts +++ b/src/server/api/get-params.ts @@ -12,7 +12,9 @@ export default function (defs: T, params: any): [{ Object.keys(defs.params).some(k => { const [v, e] = defs.params[k].get(params[k]); if (e) { - err = e; + err = new Error(e.message); + err.name = 'INVALID_PARAM'; + (err as any).param = k; return true; } else { if (v === undefined && defs.params[k].data.default) {