-
Notifications
You must be signed in to change notification settings - Fork 0
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
4 changed files
with
43 additions
and
26 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,32 +1,37 @@ | ||
import {type Result,ok,err} from "npm:neverthrow@8.0.0"; | ||
// deno-lint-ignore-file no-explicit-any | ||
import { err, ok, type Result, ResultAsync } from "npm:neverthrow@8.0.0"; | ||
|
||
|
||
type AnyFunction=(...args:any[])=>any; | ||
export type AnyFunction = (...args: any[]) => any; | ||
|
||
/** | ||
* Wraps a function that could possibly throw | ||
* and returns a new function that uses Result<T,E> | ||
* to ensure errors are handled | ||
* | ||
* @template F the type of the function to wrap | ||
* @param fn The Function being wrapped | ||
* @returns A new function that returns a Result Type | ||
* | ||
* | ||
**/ | ||
export function makeSafeFunction<F extends AnyFunction>(fn:F):(...args:Parameters<F>)=>Result<ReturnType<F> extends Promise<infer R>?R:ReturnType<F>,Error>{ | ||
// @ts-ignore: still yet to figure this out but it works | ||
return (...args:Parameters<F>)=>{ | ||
try{ | ||
const result=fn(...args); | ||
|
||
if(result instanceof Promise){ | ||
return result.then((value)=>ok(value)).catch((error)=>err(error instanceof Error?error:new Error(String(error)))); | ||
* Wraps a function that could possibly throw | ||
* and returns a new function that uses Result<T,E> | ||
* to ensure errors are handled | ||
* | ||
* @template F the type of the function to wrap | ||
* @param fn The Function being wrapped | ||
* @returns A new function that returns a Result Type | ||
* | ||
* | ||
**/ | ||
export function makeSafeFunction<F extends AnyFunction>( | ||
fn: F, | ||
): ( | ||
...args: Parameters<F> | ||
) => ReturnType<F> extends Promise<unknown> | ||
? ResultAsync<Awaited<ReturnType<F>>, Error> | ||
: Result<ReturnType<F>, Error> { | ||
return ((...args: Parameters<F>) => { | ||
try { | ||
const result = fn(...args); | ||
if (result instanceof Promise) { | ||
return ResultAsync.fromPromise(result, (error) => | ||
error instanceof Error ? error : new Error(String(error)), | ||
); | ||
} | ||
|
||
return ok(result); | ||
}catch(error){ | ||
return err(error instanceof Error?error:new Error(String(error))) | ||
} catch (error) { | ||
return err(error instanceof Error ? error : new Error(String(error))); | ||
} | ||
} | ||
}) as any; | ||
} |
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,11 @@ | ||
import { makeSafeFunction } from "./index.ts"; | ||
|
||
const unsafeDivide = (a: number, b: number) => { | ||
if (b === 0) throw new Error("Zero Division Error"); | ||
return a / b; | ||
}; | ||
|
||
const safeDivide = makeSafeFunction(unsafeDivide); | ||
|
||
safeDivide(1, 2).match(console.log, console.error); | ||
safeDivide(1, 0).match(console.log, console.error); |