From e431e0e04413e6653d01be8a77cd3ccc15b1870e Mon Sep 17 00:00:00 2001 From: meister03 <69507874+meister03@users.noreply.github.com> Date: Thu, 15 Sep 2022 17:23:49 +0200 Subject: [PATCH] fix: Rest proxy error (#2471) * Rebase Update on main * Rebase Update on main * Rebase Update on main * Fix deno fmt Co-authored-by: meister03 --- rest/runMethod.ts | 12 +++++++----- template/bigbot/src/rest/index.ts | 12 ++++++------ 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/rest/runMethod.ts b/rest/runMethod.ts index 71e7aa92f..974082395 100644 --- a/rest/runMethod.ts +++ b/rest/runMethod.ts @@ -23,6 +23,10 @@ export async function runMethod( }`, ); + const errorStack = new Error("Location:"); + // @ts-ignore Breaks deno deploy. Luca said add ts-ignore until it's fixed + Error.captureStackTrace(errorStack); + // For proxies we don't need to do any of the legwork so we just forward the request if (!baseEndpoints.BASE_URL.startsWith(BASE_URL) && route[0] === "/") { const result = await fetch(`${baseEndpoints.BASE_URL}${route}`, { @@ -36,16 +40,14 @@ export async function runMethod( if (!result.ok) { const err = await result.json().catch(() => {}); - throw new Error(`Error: ${err.message ?? result.statusText}`); + // Legacy Handling to not break old code or when body is missing + if (!err.body) throw new Error(`Error: ${err.message ?? result.statusText}`); + throw rest.convertRestError(errorStack, err); } return result.status !== 204 ? await result.json() : undefined; } - const errorStack = new Error("Location:"); - // @ts-ignore Breaks deno deploy. Luca said add ts-ignore until it's fixed - Error.captureStackTrace(errorStack); - // No proxy so we need to handle all rate limiting and such return new Promise((resolve, reject) => { rest.processRequest( diff --git a/template/bigbot/src/rest/index.ts b/template/bigbot/src/rest/index.ts index 50040fad0..5b5a01752 100644 --- a/template/bigbot/src/rest/index.ts +++ b/template/bigbot/src/rest/index.ts @@ -55,6 +55,11 @@ if (INFLUX_TOKEN) { }, 30000); } +rest.convertRestError = (errorStack, data) => { + if (!data) return { message: errorStack.message }; + return { ...data, message: errorStack.message }; +}; + const app = express(); app.use( @@ -105,12 +110,7 @@ async function handleRequest(req: Request, res: Response) { } } catch (error: any) { console.log(error); - if (error?.code) res.status(500).json(error); - else { - res.status(500).json({ - error: error.message ?? "No error found at all what the hell discord.", - }); - } + res.status(500).json(error); } }