File tree 5 files changed +64
-0
lines changed
5 files changed +64
-0
lines changed Original file line number Diff line number Diff line change 6
6
7
7
A utility pack for handling error.
8
8
9
+ ## attempt
10
+
11
+ ` attempt ` is a function that executes a function and returns the result
12
+ (` [error: unknown, value: T] ` ). If the function is successful, it returns
13
+ ` [undefined, value] ` . If the function throws an error, it returns
14
+ ` [error, undefined] ` .
15
+
16
+ ``` ts
17
+ import { assertEquals } from " @std/assert" ;
18
+ import { attempt } from " @core/errorutil/attempt" ;
19
+
20
+ assertEquals (attempt (() => 42 ), [undefined , 42 ]);
21
+ assertEquals (
22
+ attempt (() => {
23
+ throw " err" ;
24
+ }),
25
+ [" err" , undefined ],
26
+ );
27
+ ```
28
+
9
29
## ErrorObject
10
30
11
31
` ErrorObject ` is a class that wraps an error object for serialization. It is
Original file line number Diff line number Diff line change
1
+ export type Success < T > = [ error : undefined , value : T ] ;
2
+ export type Failure < E > = [ error : E , value : undefined ] ;
3
+ export type Result < T , E > = Success < T > | Failure < E > ;
4
+
5
+ /**
6
+ * Attempt to execute a function and return a Result<T, E>.
7
+ *
8
+ * @param fn - The function to execute.
9
+ * @returns A Result<T, E> where T is the return type of the function and E is the error type.
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * import { attempt } from "@core/errorutil/attempt";
14
+ *
15
+ * console.log(attempt(() => 1)); // [undefined, 1]
16
+ * console.log(attempt(() => { throw "err" })); // ["err", undefined]
17
+ * ```
18
+ */
19
+ export function attempt < T , E > ( fn : ( ) => T ) : Result < T , E > {
20
+ try {
21
+ return [ undefined , fn ( ) ] ;
22
+ } catch ( e ) {
23
+ return [ e , undefined ] ;
24
+ }
25
+ }
Original file line number Diff line number Diff line change
1
+ import { test } from "@cross/test" ;
2
+ import { assertEquals } from "@std/assert" ;
3
+ import { attempt } from "./attempt.ts" ;
4
+
5
+ test ( "attempt should return a Success<T> when the function is successful" , ( ) => {
6
+ const result = attempt ( ( ) => 1 ) ;
7
+ assertEquals ( result , [ undefined , 1 ] ) ;
8
+ } ) ;
9
+
10
+ test ( "attempt should return a Failure<E> when the function is failed" , ( ) => {
11
+ const result = attempt ( ( ) => {
12
+ throw "err" ;
13
+ } ) ;
14
+ assertEquals ( result , [ "err" , undefined ] ) ;
15
+ } ) ;
Original file line number Diff line number Diff line change 3
3
"version" : " 0.0.0" ,
4
4
"exports" : {
5
5
"." : " ./mod.ts" ,
6
+ "./attempt" : " ./attempt.ts" ,
6
7
"./error-object" : " ./error_object.ts" ,
7
8
"./raise" : " ./raise.ts" ,
8
9
"./try-or" : " ./try_or.ts" ,
20
21
" LICENSE"
21
22
],
22
23
"exclude" : [
24
+ " **/*_bench.ts" ,
23
25
" **/*_test.ts" ,
24
26
" .*"
25
27
]
26
28
},
27
29
"imports" : {
28
30
"@core/errorutil" : " ./mod.ts" ,
31
+ "@core/errorutil/attempt" : " ./attempt.ts" ,
29
32
"@core/errorutil/error-object" : " ./error_object.ts" ,
30
33
"@core/errorutil/raise" : " ./raise.ts" ,
31
34
"@core/errorutil/try-or" : " ./try_or.ts" ,
Original file line number Diff line number Diff line change
1
+ export * from "./attempt.ts" ;
1
2
export * from "./error_object.ts" ;
2
3
export * from "./raise.ts" ;
3
4
export * from "./try_or.ts" ;
You can’t perform that action at this time.
0 commit comments