safeStrategy()
safeStrategy<
In,Out>(strategy):Strategy<In,Result<Out,Error>>
Wraps a strategy to return a zygos Result instead of throwing.
Catches any exception and wraps it in Err<Error>.
DEPRECATED
Use Result.fromThrowable from @zygos/result/result instead.
import { Result } from "@zygos/result/result";
const safeParseJson = Result.fromThrowable(
(input: string) => JSON.parse(input),
(e) => e instanceof Error ? e : new Error(String(e)),
);
safeParseJson('{"ok":true}'); // Ok({ ok: true })
safeParseJson("invalid"); // Err(SyntaxError(...))
Type Parametersβ
In: Inβ
The input type
Out: Outβ
The output type
Parametersβ
strategy: Strategy<In, Out>β
The strategy to make safe
Returns: Strategy<In, Result<Out, Error>>β
A new strategy returning Result<Out, Error>
See Alsoβ
Full explanation, examples and live demo
Sinceβ
2.4.0