[API reference](https://evolu.dev/docs/api-reference) › [@evolu/common](https://evolu.dev/docs/api-reference/common) › [Result](https://evolu.dev/docs/api-reference/common/Result) › trySync

## Call Signature

```ts
function trySync<T>(fn: () => T): Result<T, unknown>;
```

Defined in: [packages/common/src/Result.ts:618](https://github.com/evoluhq/evolu/blob/037c390af081e9944d616ff298a729515c5c5ab7/packages/common/src/Result.ts#L618)

Wraps a synchronous function that may throw, returning a [Result](https://evolu.dev/docs/api-reference/common/Result/type-aliases/Result).

When provided, `mapError` converts the caught `unknown` value into a typed
domain error.

Some APIs throw for both recoverable and unrecoverable errors. In that case,
convert only the errors the caller can recover from and rethrow the rest. The
`trySync` error mapper propagates rethrown values instead of converting them
to [Err](https://evolu.dev/docs/api-reference/common/Result/interfaces/Err).

### Example

```ts
import {
  assert,
  assertErr,
  assertEqual,
  assertOk,
  assertType,
  trySync,
  type Result,
  type Typed,
} from "@evolu/common";

class LegacySeatUnavailableError extends Error {}

const legacyReserveSeat = (seat: string): void => {
  if (seat === "A1") throw new LegacySeatUnavailableError();
  if (seat === "B1") throw new Error("Database error");
};

const reserveSeat = (seat: string): Result<void, SeatUnavailableError> =>
  trySync(
    () => legacyReserveSeat(seat),
    (error) => {
      if (error instanceof LegacySeatUnavailableError) {
        return { type: "SeatUnavailable", seat };
      }
      throw error;
    },
  );

interface SeatUnavailableError extends Typed<"SeatUnavailable"> {
  readonly seat: string;
}

const result = reserveSeat("B2");
assertType<Result<void, SeatUnavailableError>, typeof result>();
assertOk(result, undefined);
assertErr(reserveSeat("A1"), { type: "SeatUnavailable", seat: "A1" });
const databaseError = trySync(() => reserveSeat("B1"));
assertErr(databaseError);
assert(databaseError.error instanceof Error, "Expected an Error.");
assertEqual(databaseError.error.message, "Database error");
```

## Call Signature

```ts
function trySync<T, E>(
  fn: () => T,
  mapError: (error: unknown) => E,
): Result<T, E>;
```

Defined in: [packages/common/src/Result.ts:621](https://github.com/evoluhq/evolu/blob/037c390af081e9944d616ff298a729515c5c5ab7/packages/common/src/Result.ts#L621)

Maps caught exceptions to a typed error.