UNPKG

@itsezz/try-catch

Version:

A TypeScript utility for elegant error handling with Result types

1 lines 8.32 kB
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["/*!\r\n * Represents a successful operation result\r\n * @template T The type of the successful data\r\n */\r\nexport type Success<T> = {\r\n\tdata: T;\r\n\terror?: never;\r\n};\r\n\r\n/*!\r\n * Represents a failed operation result\r\n * @template E The type of the error\r\n */\r\nexport type Failure<E> = {\r\n\tdata?: never;\r\n\terror: E;\r\n};\r\n\r\n/*!\r\n * Union type representing either a successful or failed operation result\r\n * @template T The type of the successful data\r\n * @template E The type of the error, defaults to Error\r\n */\r\nexport type Result<T, E = Error> = Success<T> | Failure<E>;\r\n\r\n/*!\r\n * Represents a value that might be a Promise or a direct value\r\n * @template T The type of the value\r\n */\r\nexport type MaybePromise<T> = T | Promise<T>;\r\n\r\n/*!\r\n * Type guard to check if a result represents a successful operation\r\n *\r\n * @template T The type of the successful data\r\n * @template E The type of the error\r\n * @param result The result to check\r\n * @returns True if the result represents a successful operation\r\n */\r\nexport const isSuccess = <T, E>(result: Result<T, E>): result is Success<T> => {\r\n\treturn 'data' in result;\r\n};\r\n\r\n/*!\r\n * Type guard to check if a result represents a failed operation\r\n *\r\n * @template T The type of the successful data\r\n * @template E The type of the error\r\n * @param result The result to check\r\n * @returns True if the result represents a failed operation\r\n */\r\nexport const isError = <T, E>(result: Result<T, E>): result is Failure<E> => {\r\n\treturn 'error' in result;\r\n};\r\n\r\n/*!\r\n * Creates a success result with the given data\r\n *\r\n * @template T The type of the successful data\r\n * @param data The data to include in the success result\r\n * @returns A Success object containing the data\r\n */\r\nexport const success = <T>(data: T): Success<T> => {\r\n\treturn { data };\r\n};\r\n\r\n/*!\r\n * Creates a failure result with the given error\r\n *\r\n * @template E The type of the error\r\n * @param error The error to include in the failure result\r\n * @returns A Failure object containing the error\r\n */\r\nexport const failure = <E>(error: E): Failure<E> => {\r\n\treturn { error };\r\n};\r\n\r\n/*!\r\n * Safely executes a function or awaits a promise, capturing any errors\r\n *\r\n * This utility provides a consistent way to handle both synchronous and asynchronous\r\n * operations that might throw errors. It returns a Result object that can be checked\r\n * with isSuccess or isError.\r\n *\r\n * @template T The type of the successful result\r\n * @template E The type of the error, defaults to unknown\r\n * @param fnOrPromise The function to execute, promise to await, or async function to call\r\n * @returns A Result object for synchronous functions or Promise<Result> for promises and async functions, containing either data or error\r\n */\r\nexport function tryCatch<T, E = unknown>(fn: () => Promise<T>): Promise<Result<T, E>>;\r\nexport function tryCatch<T, E = unknown>(fn: () => T): Result<T, E>;\r\nexport function tryCatch<T, E = unknown>(promise: Promise<T>): Promise<Result<T, E>>;\r\nexport function tryCatch<T, E = unknown>(\r\n\tfnOrPromise: Promise<T> | (() => MaybePromise<T>)\r\n): Result<T, E> | Promise<Result<T, E>> {\r\n\tif (typeof fnOrPromise === 'function') {\r\n\t\ttry {\r\n\t\t\tconst result = fnOrPromise();\r\n\r\n\t\t\tif (result instanceof Promise) {\r\n\t\t\t\treturn result.then((data) => ({ data })).catch((error) => ({ error: error as E }));\r\n\t\t\t}\r\n\r\n\t\t\treturn { data: result };\r\n\t\t} catch (error) {\r\n\t\t\treturn { error: error as E };\r\n\t\t}\r\n\t}\r\n\r\n\treturn fnOrPromise.then((data) => ({ data })).catch((error) => ({ error: error as E }));\r\n}\r\n\r\n/*!\r\n * Safely executes a synchronous function, capturing any errors\r\n *\r\n * This utility is specifically for synchronous operations that might throw errors.\r\n * It guarantees a synchronous Result return type, never a Promise.\r\n *\r\n * @template T The type of the successful result\r\n * @template E The type of the error, defaults to unknown\r\n * @param fn The synchronous function to execute\r\n * @returns A Result object containing either data or error\r\n */\r\nexport function tryCatchSync<T, E = unknown>(fn: () => T): Result<T, E> {\r\n\ttry {\r\n\t\tconst result = fn();\r\n\t\treturn { data: result };\r\n\t} catch (error) {\r\n\t\treturn { error: error as E };\r\n\t}\r\n}\r\n\r\n/*!\r\n * Safely executes an asynchronous function or awaits a promise, capturing any errors\r\n *\r\n * This utility is specifically for asynchronous operations that might throw errors.\r\n * It guarantees a Promise<Result> return type.\r\n *\r\n * @template T The type of the successful result\r\n * @template E The type of the error, defaults to unknown\r\n * @param fnOrPromise The async function to execute or promise to await\r\n * @returns A Promise<Result> containing either data or error\r\n */\r\nexport async function tryCatchAsync<T, E = unknown>(fn: () => Promise<T>): Promise<Result<T, E>>;\r\nexport async function tryCatchAsync<T, E = unknown>(promise: Promise<T>): Promise<Result<T, E>>;\r\nexport async function tryCatchAsync<T, E = unknown>(\r\n\tfnOrPromise: Promise<T> | (() => Promise<T>)\r\n): Promise<Result<T, E>> {\r\n\ttry {\r\n\t\tconst data = typeof fnOrPromise === 'function' ? await fnOrPromise() : await fnOrPromise;\r\n\t\treturn { data };\r\n\t} catch (error) {\r\n\t\treturn { error: error as E };\r\n\t}\r\n}\r\n\r\n/*!\r\n * Short alias for tryCatch - safely executes a function or awaits a promise\r\n *\r\n * @template T The type of the successful result\r\n * @template E The type of the error, defaults to unknown\r\n * @param fnOrPromise The function to execute, promise to await, or async function to call\r\n * @returns A Result object for synchronous functions or Promise<Result> for promises and async functions, containing either data or error\r\n */\r\nexport const t = tryCatch;\r\n\r\n/*!\r\n * Short alias for tryCatchSync - safely executes a synchronous function\r\n *\r\n * @template T The type of the successful result\r\n * @template E The type of the error, defaults to unknown\r\n * @param fn The synchronous function to execute\r\n * @returns A Result object containing either data or error\r\n */\r\nexport const tc = tryCatchSync;\r\n\r\n/*!\r\n * Short alias for tryCatchAsync - safely executes an async function or awaits a promise\r\n *\r\n * @template T The type of the successful result\r\n * @template E The type of the error, defaults to unknown\r\n * @param fnOrPromise The async function to execute or promise to await\r\n * @returns A Promise<Result> containing either data or error\r\n */\r\nexport const tca = tryCatchAsync;\r\n"],"mappings":";AAAA;AAAA;AAAA;AAAA;AASA;AAAA;AAAA;AAAA;AASA;AAAA;AAAA;AAAA;AAAA;AAOA;AAAA;AAAA;AAAA;AAMA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAQO,IAAM,YAA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