Skip to content

Commit

Permalink
better error handle
Browse files Browse the repository at this point in the history
  • Loading branch information
Sanster committed Nov 23, 2024
1 parent b7699a0 commit 4cec8de
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions web_app/src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<ServerConfig> {
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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<Filename[]> {
Expand All @@ -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}`)
Expand Down Expand Up @@ -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)
}

0 comments on commit 4cec8de

Please sign in to comment.