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