-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { Ok, Err } from "./Result"; | ||
|
||
export type Fn = () => any; | ||
export type FnAsync = () => Promise<any>; | ||
|
||
/** | ||
* Handle sync try/catch. | ||
* | ||
* ```ts | ||
* import { TryCatch, TryCatchAsync } from 'some.js'; | ||
* | ||
* TryCatch( | ||
* (a: number, b: number) => a + b, | ||
* 10, | ||
* 20, | ||
* ) // returns Ok(30) | ||
* ``` | ||
* | ||
* @export | ||
* @template FnTry | ||
* @template K | ||
* @param {FnTry} fnTry | ||
* @param {...K} args | ||
* @returns {(Ok<ReturnType<FnTry>> | Err)} | ||
*/ | ||
export function TryCatch<FnTry extends (...args: K) => any, K extends any[]>( | ||
fnTry: FnTry, | ||
...args: K | ||
): Ok<ReturnType<FnTry>> | Err { | ||
try { | ||
return Ok(fnTry.apply(null, args)); | ||
} catch (error) { | ||
return Err(error.message); | ||
} | ||
} | ||
|
||
/** | ||
* Handle sync try/catch. | ||
* | ||
* ```ts | ||
* TryCatchAsync( | ||
* (url: string) => fetch(url).then(r => r.json()), | ||
* 'https://reqres.in/api/users?page=2' | ||
* ) // returns Ok({ some json data here }) | ||
* | ||
* ``` | ||
* | ||
* @export | ||
* @template FnTry | ||
* @template K | ||
* @param {FnTry} fnTry | ||
* @param {...K} args | ||
* @returns {(Promise<Ok<ReturnType<FnTry>> | Err>)} | ||
*/ | ||
export async function TryCatchAsync< | ||
FnTry extends (...args: K) => Promise<any>, | ||
K extends any[] | ||
>(fnTry: FnTry, ...args: K): Promise<Ok<ReturnType<FnTry>> | Err> { | ||
try { | ||
return Ok(await fnTry.apply(null, args)); | ||
} catch (error) { | ||
return Err(error.message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { TryCatch, TryCatchAsync } from "../TryCatch"; | ||
import { Ok, Err } from "../Result"; | ||
|
||
describe(`TryCatch`, () => { | ||
it("returns the ok fn if does not catch", () => { | ||
expect(TryCatch(() => 10)).toStrictEqual(Ok(10)); | ||
}); | ||
|
||
it("can receive a number N of arguments", () => { | ||
expect( | ||
TryCatch((a: number, b: number) => `received ${a} and ${b}`, 10, 20) | ||
).toStrictEqual(Ok(`received 10 and 20`)); | ||
}); | ||
|
||
it("returns Err if the tryFn catches", () => { | ||
expect( | ||
TryCatch( | ||
// @ts-ignore | ||
() => a + 1 | ||
) | ||
).toStrictEqual(Err("a is not defined")); | ||
}); | ||
|
||
it("handles async functions", async () => { | ||
expect( | ||
await TryCatchAsync( | ||
(a: number, b: number) => Promise.resolve(a + b), | ||
10, | ||
20 | ||
) | ||
).toStrictEqual(Ok(30)); | ||
|
||
expect( | ||
await TryCatchAsync( | ||
// @ts-ignore | ||
() => b * 20 | ||
) | ||
).toStrictEqual(Err("b is not defined")); | ||
}); | ||
}); |