@@ -7,40 +7,94 @@ type DataErrorTuple<T, E> = Branded<
7
7
DisableArrayMethods < [ data : T , error : E ] & never [ ] >
8
8
> ;
9
9
10
+ /**
11
+ * Represents a successful result where `data` is present and `error` is `null`.
12
+ */
10
13
export type Success < T > = DataErrorTuple < T , null > ;
14
+
15
+ /**
16
+ * Represents a failure result where `error` contains an error instance and `data` is `null`.
17
+ */
11
18
export type Failure < E extends Error > = DataErrorTuple < null , E | Error > ;
19
+
20
+ /**
21
+ * Represents the result of an operation that can either succeed with `T` or fail with `E`.
22
+ */
12
23
export type Result < T , E extends Error > = Success < T > | Failure < E > ;
13
24
25
+ /**
26
+ * Resolves the return type based on whether `T` is a promise:
27
+ * - If `T` is a `Promise<U>`, returns `Promise<Result<U, E>>`.
28
+ * - Otherwise, returns `Result<T, E>`.
29
+ */
14
30
export type TryCatchResult < T , E extends Error > = T extends Promise < infer U >
15
31
? Promise < Result < U , E > >
16
32
: Result < T , E > ;
17
33
34
+ /**
35
+ * Function type for handling try-catch logic.
36
+ *
37
+ * @template E_ Default error type.
38
+ * @template T The return type of the function being executed.
39
+ * @template E The error type that extends the default error type.
40
+ *
41
+ * @param fn A function, promise, or value to execute within a try-catch block.
42
+ * @param operationName Optional name added to `error.message` for better debugging and context.
43
+ */
18
44
export type TryCatchFunc < E_ extends Error = Error > = < T , E extends Error = E_ > (
19
45
fn : T | ( ( ) => T ) ,
20
46
operationName ?: string ,
21
47
) => TryCatchResult < T , E > ;
22
48
49
+ /**
50
+ * A utility for handling synchronous and asynchronous operations within a try-catch block.
51
+ *
52
+ * @template F The function type for try-catch execution.
53
+ * @template E_ The base error type.
54
+ */
23
55
export type TryCatch <
24
56
F extends TryCatchFunc = TryCatchFunc ,
25
57
E_ extends Error = Error ,
26
58
> = F & {
59
+ /**
60
+ * Executes a synchronous function inside a try-catch block.
61
+ *
62
+ * @param fn The function to execute.
63
+ * @param operationName Optional name added to `error.message` for better debugging and context.
64
+ * @returns A `Result<T, E>` indicating success or failure.
65
+ */
27
66
sync : < T , E extends Error = E_ > (
28
67
fn : ( ) => T ,
29
68
operationName ?: string ,
30
69
) => Result < T , E > ;
70
+
71
+ /**
72
+ * Executes an asynchronous function inside a try-catch block.
73
+ *
74
+ * @param fn The function or promise to execute.
75
+ * @param operationName Optional name added to `error.message` for better debugging and context.
76
+ * @returns A `Promise<Result<T, E>>` indicating success or failure.
77
+ */
31
78
async : < T , E extends Error = E_ > (
32
79
fn : Promise < T > | ( ( ) => Promise < T > ) ,
33
80
operationName ?: string ,
34
81
) => Promise < Result < T , E > > ;
82
+
83
+ /**
84
+ * Creates a new `TryCatch` instance that handles additional error types.
85
+ *
86
+ * @template E Extends the existing error type.
87
+ * @returns A new `TryCatch` instance with extended error handling capabilities.
88
+ */
35
89
errors : < E extends Error > ( ) => TryCatch < TryCatchFunc < E | E_ > , E | E_ > ;
36
90
} ;
37
91
38
92
/**
39
93
* tryCatch - Error handling that can be synchronous or asynchronous
40
94
* based on the input function.
41
95
*
42
- * @param fn Function to execute, Promise or value.
43
- * @param operationName Optional name for context.
96
+ * @param fn A function, promise, or value to execute within a try-catch block .
97
+ * @param operationName Optional name added to `error.message` for better debugging and context.
44
98
* @returns A Result, or a Promise resolving to a Result, depending on fn.
45
99
*/
46
100
export const tryCatch : TryCatch = < T , E extends Error = Error > (
0 commit comments