Skip to content

Commit

Permalink
trying to fix issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Inalegwu committed Sep 16, 2024
1 parent b77258e commit 65918ae
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 26 deletions.
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@disgruntleddevs/make-safe-func",
"description": "Finally be sure your function don't throw",
"license": "MIT",
"version": "0.0.5",
"version": "0.0.6",
"exports": "./src/index.ts",
"tasks": {
"dev": "deno run --watch main.ts"
Expand Down
1 change: 1 addition & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 30 additions & 25 deletions src/index.ts
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;
}
11 changes: 11 additions & 0 deletions src/test.ts
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);

0 comments on commit 65918ae

Please sign in to comment.