Function: fromThrowable()
Call Signature
fromThrowable<
FnReturn
,CatchReturn
,Args
>(fn
,onErr
): (...args
) =>Result
<FnReturn
|UnwrapOk
<CatchReturn
>,InferValueAsErr
<CatchReturn
>>
Defined in: index.ts:522
Converts a function that might throw into a function that returns Results. Useful for wrapping existing throwing functions to work with Result-based error handling.
Type Parameters
FnReturn
FnReturn
The return type of the original function
CatchReturn
CatchReturn
The type returned by the catch handler
Args
Args
extends unknown
[] = []
The argument types of the function
Parameters
fn
(...args
) => NonPromise
<FnReturn
>
The function that might throw
onErr
(error
) => NonPromise
<CatchReturn
>
Value or function to handle caught errors
Returns
A new function that returns Results instead of throwing
(...
args
):Result
<FnReturn
|UnwrapOk
<CatchReturn
>,InferValueAsErr
<CatchReturn
>>
Parameters
args
...Args
Returns
Result
<FnReturn
| UnwrapOk
<CatchReturn
>, InferValueAsErr
<CatchReturn
>>
Example
const safeParse = fromThrowable(
(input: string) => JSON.parse(input),
(error) => `Parse error: ${error.message}`
);
safeParse('{"valid": true}'); // Ok<{valid: boolean}>
safeParse('invalid json'); // Err<string>
const safeDivide = fromThrowable(
(a: number, b: number) => { if (b === 0) throw new Error(); return a / b; },
"Division error"
);
safeDivide(10, 2); // Ok<number>
safeDivide(10, 0); // Err<"Division error">
Call Signature
fromThrowable<
FnReturn
,CatchReturn
,Args
>(fn
,onErr
): (...args
) =>Result
<FnReturn
|UnwrapOk
<CatchReturn
>,InferValueAsErr
<CatchReturn
>>
Defined in: index.ts:526
Converts a function that might throw into a function that returns Results. Useful for wrapping existing throwing functions to work with Result-based error handling.
Type Parameters
FnReturn
FnReturn
The return type of the original function
CatchReturn
CatchReturn
The type returned by the catch handler
Args
Args
extends unknown
[] = []
The argument types of the function
Parameters
fn
(...args
) => NonPromise
<FnReturn
>
The function that might throw
onErr
NonPromise
<CatchReturn
>
Value or function to handle caught errors
Returns
A new function that returns Results instead of throwing
(...
args
):Result
<FnReturn
|UnwrapOk
<CatchReturn
>,InferValueAsErr
<CatchReturn
>>
Parameters
args
...Args
Returns
Result
<FnReturn
| UnwrapOk
<CatchReturn
>, InferValueAsErr
<CatchReturn
>>
Example
const safeParse = fromThrowable(
(input: string) => JSON.parse(input),
(error) => `Parse error: ${error.message}`
);
safeParse('{"valid": true}'); // Ok<{valid: boolean}>
safeParse('invalid json'); // Err<string>
const safeDivide = fromThrowable(
(a: number, b: number) => { if (b === 0) throw new Error(); return a / b; },
"Division error"
);
safeDivide(10, 2); // Ok<number>
safeDivide(10, 0); // Err<"Division error">
Call Signature
fromThrowable<
FnReturn
,CatchReturn
,Args
>(fn
,onErr
): (...args
) =>Promise
<Result
<FnReturn
|UnwrapOk
<CatchReturn
>,InferValueAsErr
<CatchReturn
>>>
Defined in: index.ts:530
Converts a function that might throw into a function that returns Results. Useful for wrapping existing throwing functions to work with Result-based error handling.
Type Parameters
FnReturn
FnReturn
The return type of the original function
CatchReturn
CatchReturn
The type returned by the catch handler
Args
Args
extends unknown
[] = []
The argument types of the function
Parameters
fn
(...args
) => OrPromise
<FnReturn
>
The function that might throw
onErr
OrFunction
<OrPromise
<CatchReturn
>, [unknown
]>
Value or function to handle caught errors
Returns
A new function that returns Results instead of throwing
(...
args
):Promise
<Result
<FnReturn
|UnwrapOk
<CatchReturn
>,InferValueAsErr
<CatchReturn
>>>
Parameters
args
...Args
Returns
Promise
<Result
<FnReturn
| UnwrapOk
<CatchReturn
>, InferValueAsErr
<CatchReturn
>>>
Example
const safeParse = fromThrowable(
(input: string) => JSON.parse(input),
(error) => `Parse error: ${error.message}`
);
safeParse('{"valid": true}'); // Ok<{valid: boolean}>
safeParse('invalid json'); // Err<string>
const safeDivide = fromThrowable(
(a: number, b: number) => { if (b === 0) throw new Error(); return a / b; },
"Division error"
);
safeDivide(10, 2); // Ok<number>
safeDivide(10, 0); // Err<"Division error">