UNPKG

@itsezz/try-catch

Version:

A TypeScript utility for elegant error handling with Result types

198 lines (197 loc) 6.49 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var index_exports = {}; __export(index_exports, { failure: () => failure, isError: () => isError, isSuccess: () => isSuccess, success: () => success, t: () => t, tc: () => tc, tca: () => tca, tryCatch: () => tryCatch, tryCatchAsync: () => tryCatchAsync, tryCatchSync: () => tryCatchSync }); module.exports = __toCommonJS(index_exports); /*! * Represents a successful operation result * @template T The type of the successful data */ /*! * Represents a failed operation result * @template E The type of the error */ /*! * Union type representing either a successful or failed operation result * @template T The type of the successful data * @template E The type of the error, defaults to Error */ /*! * Represents a value that might be a Promise or a direct value * @template T The type of the value */ /*! * Type guard to check if a result represents a successful operation * * @template T The type of the successful data * @template E The type of the error * @param result The result to check * @returns True if the result represents a successful operation */ var isSuccess = (result) => { return "data" in result; }; /*! * Type guard to check if a result represents a failed operation * * @template T The type of the successful data * @template E The type of the error * @param result The result to check * @returns True if the result represents a failed operation */ var isError = (result) => { return "error" in result; }; /*! * Creates a success result with the given data * * @template T The type of the successful data * @param data The data to include in the success result * @returns A Success object containing the data */ var success = (data) => { return { data }; }; /*! * Creates a failure result with the given error * * @template E The type of the error * @param error The error to include in the failure result * @returns A Failure object containing the error */ var failure = (error) => { return { error }; }; /*! * Safely executes a function or awaits a promise, capturing any errors * * This utility provides a consistent way to handle both synchronous and asynchronous * operations that might throw errors. It returns a Result object that can be checked * with isSuccess or isError. * * @template T The type of the successful result * @template E The type of the error, defaults to unknown * @param fnOrPromise The function to execute, promise to await, or async function to call * @returns A Result object for synchronous functions or Promise<Result> for promises and async functions, containing either data or error */ function tryCatch(fnOrPromise) { if (typeof fnOrPromise === "function") { try { const result = fnOrPromise(); if (result instanceof Promise) { return result.then((data) => ({ data })).catch((error) => ({ error })); } return { data: result }; } catch (error) { return { error }; } } return fnOrPromise.then((data) => ({ data })).catch((error) => ({ error })); } /*! * Safely executes a synchronous function, capturing any errors * * This utility is specifically for synchronous operations that might throw errors. * It guarantees a synchronous Result return type, never a Promise. * * @template T The type of the successful result * @template E The type of the error, defaults to unknown * @param fn The synchronous function to execute * @returns A Result object containing either data or error */ function tryCatchSync(fn) { try { const result = fn(); return { data: result }; } catch (error) { return { error }; } } /*! * Safely executes an asynchronous function or awaits a promise, capturing any errors * * This utility is specifically for asynchronous operations that might throw errors. * It guarantees a Promise<Result> return type. * * @template T The type of the successful result * @template E The type of the error, defaults to unknown * @param fnOrPromise The async function to execute or promise to await * @returns A Promise<Result> containing either data or error */ async function tryCatchAsync(fnOrPromise) { try { const data = typeof fnOrPromise === "function" ? await fnOrPromise() : await fnOrPromise; return { data }; } catch (error) { return { error }; } } /*! * Short alias for tryCatch - safely executes a function or awaits a promise * * @template T The type of the successful result * @template E The type of the error, defaults to unknown * @param fnOrPromise The function to execute, promise to await, or async function to call * @returns A Result object for synchronous functions or Promise<Result> for promises and async functions, containing either data or error */ var t = tryCatch; /*! * Short alias for tryCatchSync - safely executes a synchronous function * * @template T The type of the successful result * @template E The type of the error, defaults to unknown * @param fn The synchronous function to execute * @returns A Result object containing either data or error */ var tc = tryCatchSync; /*! * Short alias for tryCatchAsync - safely executes an async function or awaits a promise * * @template T The type of the successful result * @template E The type of the error, defaults to unknown * @param fnOrPromise The async function to execute or promise to await * @returns A Promise<Result> containing either data or error */ var tca = tryCatchAsync; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { failure, isError, isSuccess, success, t, tc, tca, tryCatch, tryCatchAsync, tryCatchSync }); //# sourceMappingURL=index.js.map