better error handle

This commit is contained in:
Qing
2024-11-23 18:49:22 +08:00
parent b7699a0f26
commit 4cec8ded64

View File

@@ -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<ServerConfig> {
@@ -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<Filename[]> {
@@ -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)
}