mirror of
https://github.com/Radiquum/AniX.git
synced 2025-04-30 18:09:40 +05:00
refactor: improve error handling for custom player
This commit is contained in:
parent
fc8fe97da7
commit
f94b551cdf
2 changed files with 328 additions and 187 deletions
|
@ -27,6 +27,46 @@ export async function tryCatch<T, E = Error>(
|
|||
}
|
||||
}
|
||||
|
||||
export async function tryCatchPlayer<T, E = Error>(
|
||||
promise: Promise<any>
|
||||
): Promise<Result<any, any>> {
|
||||
try {
|
||||
const res: Awaited<Response> = await promise;
|
||||
const data = await res.json();
|
||||
if (!res.ok) {
|
||||
if (data.message) {
|
||||
return {
|
||||
data: null,
|
||||
error: {
|
||||
message: data.message,
|
||||
code: res.status,
|
||||
},
|
||||
};
|
||||
} else if (data.detail) {
|
||||
return {
|
||||
data: null,
|
||||
error: {
|
||||
message: data.detail,
|
||||
code: res.status,
|
||||
},
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
data: null,
|
||||
error: {
|
||||
message: res.statusText,
|
||||
code: res.status,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return { data, error: null };
|
||||
} catch (error) {
|
||||
return { data: null, error: error as E };
|
||||
}
|
||||
}
|
||||
|
||||
export async function tryCatchAPI<T, E = Error>(
|
||||
promise: Promise<any>
|
||||
): Promise<Result<any, any>> {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue