/** * Walk the .cause chain of an error to find an ancestor matching a specific error class. * Checks the error itself first, then traverses .cause recursively. * Similar to Go's `errors.As`. * * @example * const notFound = findCause(err, NotFoundError) * if (notFound) { * console.log(notFound.id) // type-safe access * } * * @example * // With optional chaining * const id = findCause(err, NotFoundError)?.id */ export declare function findCause(error: Error, ErrorClass: new (...args: any[]) => T): T | undefined; /** * Any tagged error (for generic constraints) */ type AnyTaggedError = Error & { readonly _tag: string; }; /** * Any class that extends Error */ type ErrorClass = new (...args: any[]) => Error; /** * Instance type produced by TaggedError factory */ export type TaggedErrorInstance = Base & { readonly _tag: Tag; /** Stable fingerprint for error grouping in Sentry/logging. Returns [_tag]. */ readonly fingerprint: readonly [Tag]; toJSON(): object; /** Walk the .cause chain to find an ancestor matching a specific error class. */ findCause(ErrorClass: new (...args: any[]) => T): T | undefined; } & Readonly; /** * Class type produced by TaggedError factory */ export type TaggedErrorClass = { new (...args: keyof Props extends never ? [args?: {}] : [args: Props]): TaggedErrorInstance; /** Type guard for this error class */ is(value: unknown): value is TaggedErrorInstance; }; /** * Factory for tagged error classes with discriminated _tag property. * Enables exhaustive pattern matching on error unions. * * @example * class NotFoundError extends TaggedError("NotFoundError")<{ * id: string; * message: string; * }>() {} * * const err = new NotFoundError({ id: "123", message: "Not found" }); * err._tag // "NotFoundError" * err.id // "123" * * // Type guard * NotFoundError.is(err) // true * TaggedError.is(err) // true (any tagged error) * * @example * // With custom base class * class AppError extends Error { * statusCode: number = 500 * report() { console.log(this.message) } * } * * class NotFoundError extends TaggedError("NotFoundError", AppError)<{ * id: string; * message: string; * }>() { * statusCode = 404 * } * * const err = new NotFoundError({ id: "123", message: "Not found" }); * err.statusCode // 404 * err.report() // works */ export declare const TaggedError: { (tag: Tag, BaseClass?: BaseClass): = {}>() => TaggedErrorClass>; /** Type guard for any TaggedError instance */ is(value: unknown): value is AnyTaggedError; }; /** * Type guard for tagged error instances. * * @example * if (isTaggedError(value)) { value._tag } */ export declare const isTaggedError: (value: unknown) => value is AnyTaggedError; /** * Handler map with required `Error` fallback for plain Error (untagged) */ type MatchHandlersWithPlain = { [K in Extract['_tag']]: (err: Extract) => R; } & { Error: (err: Exclude extends never ? Error : Exclude) => R; }; /** * Exhaustive pattern match on error union by _tag. * The `Error` handler is always required as fallback for plain Error instances. * * @example * const message = matchError(err, { * NotFoundError: (e) => `Missing: ${e.id}`, * ValidationError: (e) => `Invalid: ${e.field}`, * Error: (e) => `Unknown error: ${e.message}`, * }); */ export declare function matchError(err: E, handlers: MatchHandlersWithPlain): R; /** * Partial pattern match with fallback for unhandled tags. * * @example * const message = matchErrorPartial(err, { * NotFoundError: (e) => `Missing: ${e.id}`, * }, (e) => `Unknown: ${e.message}`); */ export declare function matchErrorPartial(err: E, handlers: Partial>, fallback: (e: E) => R): R; /** * Base class for abort-related errors. * Extend this in custom abort errors so `isAbortError` detects them * even when wrapped in a cause chain. * * @example * class TimeoutError extends errore.createTaggedError({ * name: 'TimeoutError', * message: 'Request timed out for $operation', * extends: errore.AbortError, * }) {} * * controller.abort(new TimeoutError({ operation: 'fetch' })) */ export declare class AbortError extends Error { constructor(message?: string, options?: ErrorOptions); } /** * Check if an error (or any error in its `.cause` chain) is an abort error. * Detects native AbortError (DOMException), errore.AbortError, and any * tagged error that extends errore.AbortError. * * @example * const res = await fetch(url, { signal }) * .catch((e) => new NetworkError({ url, cause: e })) * if (errore.isAbortError(res)) { * // request was aborted — timeout, user cancel, etc. * } */ export declare function isAbortError(error: unknown): error is Error; declare const UnhandledError_base: TaggedErrorClass<"UnhandledError", { message: string; cause: unknown; }, Error>; /** * Default error type when catching unknown exceptions. */ export declare class UnhandledError extends UnhandledError_base { constructor(args: { cause: unknown; }); } export {}; //# sourceMappingURL=error.d.ts.map