/** * Error chain utilities for working with ES2022 Error.cause chains * * These utilities help extract, format, and inspect error chains for * debugging and programmatic error handling. */ /** * Options for formatting error chains */ export interface FormatErrorChainOptions { /** Include stack traces in output (default: false) */ includeStack?: boolean; /** Indentation string for nested errors (default: ' ') */ indent?: string; /** Maximum chain depth to traverse (default: 10) */ maxDepth?: number; /** Include error code if available (default: true) */ includeCode?: boolean; /** Include context if available (default: false) */ includeContext?: boolean; } /** * Structured representation of an error in a chain */ export interface ErrorChainEntry { name: string; message: string; code?: string; stack?: string; context?: Record; depth: number; } /** * Extract the full chain of errors as an array * * @param error - The error to extract the chain from * @param maxDepth - Maximum depth to traverse (default: 10) * @returns Array of errors from outermost to innermost * * @example * ```typescript * try { * await emailManager.send(email); * } catch (error) { * const chain = getErrorChain(error); * // [EmailManagerError, EmailSenderError, SendGridApiError] * } * ``` */ export declare function getErrorChain(error: Error, maxDepth?: number): Error[]; /** * Format an error chain as a human-readable string * * @param error - The error to format * @param options - Formatting options * @returns Formatted string representation of the error chain * * @example * ```typescript * console.error(formatErrorChain(error)); * // EmailManagerError: Failed to send email * // EmailSenderError: Provider rejected request * // SendGridApiError: Invalid sender identity * ``` */ export declare function formatErrorChain(error: Error, options?: FormatErrorChainOptions): string; /** * Check if any error in the chain is an instance of a specific error class * * @param error - The error to check * @param errorClass - The error class to look for * @returns true if any error in the chain matches the class * * @example * ```typescript * if (hasErrorInChain(error, RateLimitError)) { * await delay(error.context?.retryAfter ?? 1000); * return retry(); * } * ``` */ export declare function hasErrorInChain(error: Error, errorClass: new (...args: any[]) => T): boolean; /** * Find the first error in the chain that matches a specific class * * @param error - The error to search * @param errorClass - The error class to look for * @returns The matching error or undefined * * @example * ```typescript * const sendGridError = findErrorInChain(error, SendGridApiError); * if (sendGridError) { * console.log('Status code:', sendGridError.statusCode); * } * ``` */ export declare function findErrorInChain(error: Error, errorClass: new (...args: any[]) => T): T | undefined; /** * Find the first error in the chain that has a specific error code * * @param error - The error to search * @param code - The error code to look for * @returns The matching error or undefined * * @example * ```typescript * const rateLimitError = findErrorByCode(error, 'RATE_LIMITED'); * if (rateLimitError) { * await delay(rateLimitError.context?.retryAfter); * } * ``` */ export declare function findErrorByCode(error: Error, code: string): (Error & { code: string; }) | undefined; /** * Get the root cause (deepest error) in the chain * * @param error - The error to get the root cause from * @param maxDepth - Maximum depth to traverse (default: 10) * @returns The deepest error in the chain * * @example * ```typescript * const rootCause = getRootCause(error); * console.log('Original error:', rootCause.message); * ``` */ export declare function getRootCause(error: Error, maxDepth?: number): Error; /** * Convert an error chain to a structured array for JSON serialization * * @param error - The error to convert * @param options - Formatting options * @returns Array of structured error entries * * @example * ```typescript * const entries = getErrorChainEntries(error); * logger.error('Operation failed', { errorChain: entries }); * ``` */ export declare function getErrorChainEntries(error: Error, options?: Pick): ErrorChainEntry[]; /** * Check if an error is retryable based on its chain * * An error is considered retryable if any error in the chain: * - Has a `retryable` property set to true * - Has a code matching common retryable patterns (RATE_LIMITED, TIMEOUT, etc.) * * @param error - The error to check * @returns true if the error should be retried */ export declare function isRetryableError(error: Error): boolean; /** * Extract retry timing information from an error chain * * @param error - The error to extract retry info from * @returns Retry timing info or undefined if not available */ export declare function getRetryInfo(error: Error): { retryAfter?: number; maxRetries?: number; } | undefined; //# sourceMappingURL=error-chain.d.ts.map