API reference › @evolu/common › Result › trySync
function trySync<T, E>(
fn: () => T,
mapError: (error: unknown) => E,
): Result<T, E>;
Defined in: packages/common/src/Result.ts:408
Wraps a synchronous function that may throw, returning a Result.
Use this when the thrown value should become a typed, recoverable error for the caller. Do not use it for failures that should terminate the current flow and propagate to a global handler.
Example
const parseJson = (value: string): Result<unknown, ParseJsonError> =>
trySync(
() => JSON.parse(value) as unknown,
(error) => ({ type: "ParseJsonError", message: String(error) }),
);