@vsokolov/utils - v1.4.2
    Preparing search index...

    Function tryCatch

    • Wraps an operation and returns a Result object instead of throwing exceptions.

      • Sync functions return Result<T, E> directly (no Promise)
      • Async functions and direct Promises return Promise<Result<T, E>>

      Type Parameters

      • T

        The type of data returned by the successful operation

      • E extends Error = ErrorWithStatus

        The type of error returned, defaults to ErrorWithStatus

      Parameters

      • fnOrPromise: Promise<T> | (() => Promise<T>)

        A sync function, async function, or Promise to execute safely

      • Optionaloptions: TryCatchOptions<E>

        Configuration options for error handling behavior

      Returns Promise<Result<T, E>>

      Result<T, E> for sync functions, Promise<Result<T, E>> for async/Promise inputs

      const { data, error } = tryCatch(() => JSON.parse(raw));
      
      const { data, error } = await tryCatch(async () => {
      const user = await getUser();
      const profile = await getProfile(user.id);
      return { user, profile };
      });
      const { data, error } = await tryCatch(fetchUserData());
      
      class ApiError extends Error {
      constructor(message: string, public status?: number) {
      super(message);
      }
      }

      const { data, error } = await tryCatch(fetchApiData(), {
      ErrorClass: ApiError,
      defaultStatus: 500
      });
      const result = tryCatch(() => JSON.parse(raw));
      if (isFailure(result)) {
      console.error(result.error.message);
      } else {
      console.log(result.data);
      }
    • Wraps an operation and returns a Result object instead of throwing exceptions.

      • Sync functions return Result<T, E> directly (no Promise)
      • Async functions and direct Promises return Promise<Result<T, E>>

      Type Parameters

      • T

        The type of data returned by the successful operation

      • E extends Error = ErrorWithStatus

        The type of error returned, defaults to ErrorWithStatus

      Parameters

      • fn: () => T
      • Optionaloptions: TryCatchOptions<E>

        Configuration options for error handling behavior

      Returns Result<T, E>

      Result<T, E> for sync functions, Promise<Result<T, E>> for async/Promise inputs

      const { data, error } = tryCatch(() => JSON.parse(raw));
      
      const { data, error } = await tryCatch(async () => {
      const user = await getUser();
      const profile = await getProfile(user.id);
      return { user, profile };
      });
      const { data, error } = await tryCatch(fetchUserData());
      
      class ApiError extends Error {
      constructor(message: string, public status?: number) {
      super(message);
      }
      }

      const { data, error } = await tryCatch(fetchApiData(), {
      ErrorClass: ApiError,
      defaultStatus: 500
      });
      const result = tryCatch(() => JSON.parse(raw));
      if (isFailure(result)) {
      console.error(result.error.message);
      } else {
      console.log(result.data);
      }