31 lines
455 B
TypeScript
31 lines
455 B
TypeScript
|
|
export type Success<D> = {
|
|
error?: undefined | null,
|
|
data: D
|
|
}
|
|
|
|
export type Failure<E = Error> = {
|
|
data?: undefined | null,
|
|
error: E
|
|
}
|
|
|
|
/**
|
|
* Result<Error,Data>
|
|
*/
|
|
export type Result<E, D> = Failure<E> | Success<D>
|
|
|
|
export async function tryCatch<T>(func: Promise<T>): Promise<Result<Error, T>> {
|
|
try {
|
|
const res = await func;
|
|
return {
|
|
data: res
|
|
}
|
|
} catch (e: unknown) {
|
|
return {
|
|
error: e as Error
|
|
|
|
}
|
|
}
|
|
}
|
|
|