feat(utils): added in trycatch function
This commit is contained in:
28
backend/src/utils/trycatch.utlis.ts
Normal file
28
backend/src/utils/trycatch.utlis.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
type Success<T> = { data: T; error: null };
|
||||
type Failure<E> = { data: null; error: E };
|
||||
|
||||
export type Result<T, E = Error> = Success<T> | Failure<E>;
|
||||
|
||||
/**
|
||||
* A universal tryCatch wrapper that:
|
||||
* - Never throws
|
||||
* - Always resolves to Result<T,E>
|
||||
* - Allows optional error mapping function for strong typing
|
||||
*/
|
||||
|
||||
export async function tryCatch<T, E = Error>(
|
||||
promise: Promise<T>,
|
||||
onError?: (error: unknown) => E,
|
||||
): Promise<Result<T, E>> {
|
||||
try {
|
||||
const data = await promise;
|
||||
return { data, error: null };
|
||||
} catch (err: unknown) {
|
||||
const error = onError
|
||||
? onError(err)
|
||||
: err instanceof Error
|
||||
? (err as E)
|
||||
: (new Error(String(err)) as E);
|
||||
return { data: null, error };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user