2026-03-04 13:51:24 +01:00
|
|
|
|
|
|
|
|
export type Success<D> = {
|
|
|
|
|
error?: undefined | null,
|
|
|
|
|
data: D
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export type Failure<E = Error> = {
|
|
|
|
|
data?: undefined | null,
|
|
|
|
|
error: E
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-04 15:28:33 +01:00
|
|
|
/**
|
|
|
|
|
* Result<Error,Data>
|
|
|
|
|
*/
|
2026-03-04 13:51:24 +01:00
|
|
|
export type Result<E, D> = Failure<E> | Success<D>
|
|
|
|
|
|
2026-04-07 13:20:31 +02:00
|
|
|
export async function tryCatch<T>(func: Promise<T>): Promise<Result<Error, T>> {
|
2026-03-04 13:51:24 +01:00
|
|
|
try {
|
|
|
|
|
const res = await func;
|
|
|
|
|
return {
|
|
|
|
|
data: res
|
|
|
|
|
}
|
|
|
|
|
} catch (e: unknown) {
|
|
|
|
|
return {
|
2026-04-07 13:20:31 +02:00
|
|
|
error: e as Error
|
|
|
|
|
|
2026-03-04 13:51:24 +01:00
|
|
|
}
|
2026-02-16 17:31:20 +01:00
|
|
|
}
|
2026-03-04 13:51:24 +01:00
|
|
|
}
|
2026-02-16 17:31:20 +01:00
|
|
|
|