From 4cec8ded642d68ee19fc7b4f9ee0d72d34df8332 Mon Sep 17 00:00:00 2001 From: Qing Date: Sat, 23 Nov 2024 18:49:22 +0800 Subject: [PATCH] better error handle --- web_app/src/lib/api.ts | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/web_app/src/lib/api.ts b/web_app/src/lib/api.ts index a8f0b7f5..e35cccec 100644 --- a/web_app/src/lib/api.ts +++ b/web_app/src/lib/api.ts @@ -18,6 +18,13 @@ const api = axios.create({ baseURL: API_ENDPOINT, }) +const throwErrors = async (res: any) => { + const errMsg = await res.json() + throw new Error( + `${errMsg.errors}\nPlease take a screenshot of the detailed error message in your terminal` + ) +} + export default async function inpaint( imageFile: File, settings: Settings, @@ -92,8 +99,7 @@ export default async function inpaint( seed: res.headers.get("X-Seed"), } } - const errors = await res.json() - throw new Error(`Something went wrong: ${errors.errors}`) + await throwErrors(res) } export async function getServerConfig(): Promise { @@ -143,8 +149,7 @@ export async function runPlugin( const blob = await res.blob() return { blob: URL.createObjectURL(blob) } } - const errMsg = await res.json() - throw new Error(errMsg) + await throwErrors(res) } export async function getMediaFile(tab: string, filename: string) { @@ -163,8 +168,7 @@ export async function getMediaFile(tab: string, filename: string) { }) return file } - const errMsg = await res.json() - throw new Error(errMsg.errors) + await throwErrors(res) } export async function getMediaBlob(tab: string, filename: string) { @@ -180,8 +184,7 @@ export async function getMediaBlob(tab: string, filename: string) { const blob = await res.blob() return blob } - const errMsg = await res.json() - throw new Error(errMsg.errors) + await throwErrors(res) } export async function getMedias(tab: string): Promise { @@ -204,8 +207,7 @@ export async function downloadToOutput( body: fd, }) if (!res.ok) { - const errMsg = await res.text() - throw new Error(errMsg) + await throwErrors(res) } } catch (error) { throw new Error(`Something went wrong: ${error}`) @@ -245,6 +247,5 @@ export async function postAdjustMask( const blob = await res.blob() return blob } - const errMsg = await res.json() - throw new Error(errMsg) + throwErrors(res) }