File tree Expand file tree Collapse file tree 5 files changed +64
-0
lines changed Expand file tree Collapse file tree 5 files changed +64
-0
lines changed Original file line number Diff line number Diff line change 66
77A utility pack for handling error.
88
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+
929## ErrorObject
1030
1131` 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 33 "version" : " 0.0.0" ,
44 "exports" : {
55 "." : " ./mod.ts" ,
6+ "./attempt" : " ./attempt.ts" ,
67 "./error-object" : " ./error_object.ts" ,
78 "./raise" : " ./raise.ts" ,
89 "./try-or" : " ./try_or.ts" ,
2021 " LICENSE"
2122 ],
2223 "exclude" : [
24+ " **/*_bench.ts" ,
2325 " **/*_test.ts" ,
2426 " .*"
2527 ]
2628 },
2729 "imports" : {
2830 "@core/errorutil" : " ./mod.ts" ,
31+ "@core/errorutil/attempt" : " ./attempt.ts" ,
2932 "@core/errorutil/error-object" : " ./error_object.ts" ,
3033 "@core/errorutil/raise" : " ./raise.ts" ,
3134 "@core/errorutil/try-or" : " ./try_or.ts" ,
Original file line number Diff line number Diff line change 1+ export * from "./attempt.ts" ;
12export * from "./error_object.ts" ;
23export * from "./raise.ts" ;
34export * from "./try_or.ts" ;
You can’t perform that action at this time.
0 commit comments