import { type Maybe } from '../value/maybe.type'; /** * Generic function that is meant to throw an error if the input is known. Returns void otherwise. */ export type ThrowErrorFunction = (error: T) => never | void; /** * A unique identifier for a specific error. * * @semanticType * @semanticTopic identifier * @semanticTopic string */ export type StringErrorCode = string; /** * Default error code used when no specific code is provided. */ export declare const DEFAULT_READABLE_ERROR_CODE = "ERROR"; /** * An error that is identified by a unique code. */ export interface CodedError { readonly code: StringErrorCode; /** * The original error, if available. */ readonly _error?: unknown; } /** * An error with a human-readable message. */ export interface ReadableError extends Partial { readonly message?: Maybe; } /** * Checks if the error has the default error code or no code at all. * * @param error - A ReadableError or error code string to check. * @returns True if the error uses the default code or has no code. */ export declare function isDefaultReadableError(error: Maybe): boolean; /** * A ReadableError that is guaranteed to have a code. */ export type ReadableErrorWithCode = T & CodedError; /** * Creates a ReadableError with a code and optional message. * * @param code - The error code. * @param message - Optional human-readable error message. * @returns A ReadableErrorWithCode object. */ export declare function readableError(code: StringErrorCode, message?: string): ReadableErrorWithCode; /** * A ReadableError that includes additional data of type T. */ export interface ReadableDataError extends ReadableError { readonly data?: T | undefined; } /** * Wrapper around an error that contains the error data. * * Unwrapped by {@link toReadableError} only when `data` is an object. */ export interface ErrorWrapper { readonly data: ReadableError | CodedError; } /** * Union of all supported error input types for conversion functions. */ export type ErrorInput = ErrorWrapper | CodedError | ReadableError | ReadableDataError; /** * Converts any thrown value to a normalized ReadableErrorWithCode. * Handles CodedError, ErrorWrapper, BaseError, plain ReadableError inputs and primitives. * * - `null`/`undefined` returns `undefined`. * - Any other primitive (a string, number, boolean, bigint or symbol, falsy ones included) returns * `{ code: DEFAULT_READABLE_ERROR_CODE, message: String(value), _error: value }`. * - An `{ data }` wrapper is unwrapped only when `data` is a non-null, non-array object. Otherwise the input's own * message is kept, so a code-less `ServerErrorResponse` without `data` keeps its message. * * The second overload is written `Maybe>` rather than `unknown` so that an `any` argument still * resolves to the first, non-Maybe overload. * * @param inputError - The error to convert * @returns A normalized ReadableErrorWithCode */ export declare function toReadableError(inputError: ErrorInput): CodedError | ReadableErrorWithCode; export declare function toReadableError(inputError: Maybe>): Maybe; /** * Checks if an error's message contains the target string. * * @param input - Error-shaped value or message string to inspect; nullish short-circuits to false. * @param target - Substring that must appear in the resolved error message. * @returns True when the error message includes `target`. */ export declare function errorMessageContainsString(input: Maybe, target: string): boolean; /** * Function that checks if an error's message contains a specific string. */ export type ErrorMessageContainsStringFunction = (input: Maybe) => boolean; /** * Creates a function that checks if an error's message contains the target string. * * @param target - Substring that must appear in the resolved error message. * @returns Reusable predicate that yields true when the inspected error contains `target`. * * @dbxUtil * @dbxUtilCategory error * @dbxUtilKind factory * @dbxUtilTags error, message, contains, factory, predicate, regex * @dbxUtilRelated escape-string-for-regex * * @__NO_SIDE_EFFECTS__ */ export declare function errorMessageContainsStringFunction(target: string): ErrorMessageContainsStringFunction; /** * Extracts the message string from an error or returns the input if it's already a string. * * A `null` or `undefined` input is returned as-is, and an error object without a message returns `undefined`. * * @param input - The error or string to extract a message from. * @returns The error message string, or null/undefined if not available. */ export declare function messageFromError(input: Maybe): Maybe; /** * Reads a human-readable message from any thrown value, never throwing. * * - An `Error` (including `DOMException`) yields its `message`. * - Any other non-null object whose `message` is a string (a {@link ReadableError}-like or cross-realm error) yields that message. * - Anything else yields `String(error)`; when `String()` itself throws (a null-prototype object or a throwing `toString`), * it falls back to `Object.prototype.toString.call(error)`. * * This is the `unknown → string` sibling of {@link messageFromError}, for catch blocks whose caught value is `unknown`. * * @param error - The caught value to read a message from. * @returns The message text; never null or undefined. * * @dbxUtil * @dbxUtilCategory error * @dbxUtilTags error, message, unknown, catch, text, string * @dbxUtilRelated message-from-error * * @example * ```ts * readErrorMessage(new Error('boom')); // 'boom' * readErrorMessage({ message: 'readable' }); // 'readable' * readErrorMessage('plain'); // 'plain' * readErrorMessage(undefined); // 'undefined' * ``` */ export declare function readErrorMessage(error: unknown): string; /** * Every constructor NAME on a thrown value's prototype chain, leaf first, stopping at `Object.prototype` — a STRUCTURAL * identity for error classes that never set `.name` (the `openai` SDK's, whose every instance reads * `.name === 'Error'`), matched without importing them. * * Matching an ANCESTOR's name makes a future subclass match by inheritance, the way `instanceof` would, with zero * imports. The leaf name alone is not enough: a subclass such as `APIConnectionTimeoutError` extends * `APIConnectionError`, and a leaf-only check lets the parent's other subclasses fall through. * * Minified bundles that mangle class names defeat it; a `.name` the class sets itself survives minification, so a * classifier usually checks `.name` too. * * @param error - The thrown value. * @returns The chain's constructor names, leaf first. Empty for a non-object or a null-prototype object. * * @dbxUtil * @dbxUtilCategory error * @dbxUtilTags error, class, name, prototype, instanceof, structural, constructor * * @example * ```ts * class HttpError extends Error {} * class RateLimitError extends HttpError {} * * errorClassNames(new RateLimitError()); // ['RateLimitError', 'HttpError', 'Error'] * errorClassNames('boom'); // [] * ``` */ export declare function errorClassNames(error: unknown): string[];