/** * Type for a class constructor that extends {@link Error}. */ export type AnyErrorConstructor = { new (...args: any[]): T; }; export type EErrorOptions = { /** * A property indicating the specific cause of the error. * * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause */ cause?: Cause; /** * Additional data to store in the error. */ info?: T; }; /** * Normalised JSON format for errors. */ export type EErrorJSON = { name: string; message: string; originalMessage?: string; info?: any; cause?: EErrorJSON; stack?: string[]; }; /** * An extension of the built-in {@link Error} class that allows you to wrap an * existing error as the cause and add structured data to help with debugging. * * @template T The shape of any structured data to pass to the error. * @template Cause The error type that this error may wrap as a cause. Must extend the {@link Error} type. */ export declare class EError extends Error { /** * A property indicating the specific cause of the error. * * @override * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause */ readonly cause?: Cause; /** * The original message passed to the error constructor. */ readonly originalMessage: string; readonly info?: T; /** * Create an {@link EError} instance with an empty message. */ constructor(); /** * Create an {@link EError} instance with the given message. */ constructor(message: string); /** * Create an {@link EError} instance with the given message and cause. * The message of the causing error will be appended to this error's message. * * @param message The error message. * @param cause The causing error to wrap. */ constructor(message: string, cause: Cause); /** * Create an {@link EError} instance with the given message and options. * The message of the causing error, if provided, will be appended * to this error's message. * * @param message The error message. * @param options Data to pass to the error, such as `cause` and `info`. */ constructor(message: string, options: EErrorOptions); /** * Create an {@link EError} instance with the given options * and an empty message. */ constructor(options: EErrorOptions); /** * Create an {@link EError} instance with the given cause and * an empty message. */ constructor(cause: Cause); /** * Get the cause chain of the error, including the error itself. * * @param error The error to get the cause chain from. * @param filter A function to filter the causes to return. Return `true` if an error in the chain should be included in the result. */ static getCauses(error: Error | undefined, filter?: (error: Error) => boolean): Error[]; /** * Get the cause chain of the error, including the error itself. * * @param filter A function to filter the causes to return. Return `true` if an error in the chain should be included in the result. */ getCauses(filter?: (error: Error) => boolean): Error[]; /** * Find the first occurence of provided error type in the error's cause chain, * including the error itself. If not found, `null` will be returned. * * @param error The error to get the cause chain from. * @param type The error type. Must be a class definition, such as {@link Error}. */ static findCause(error: Error | undefined, type: AnyErrorConstructor): T | null; /** * Find the first occurence of provided error type in the error's cause chain * including the error itself. If not found, `null` will be returned. * * @param type The error type. Must be a class definition, such as {@link Error}. */ findCause(type: AnyErrorConstructor): T | null; /** * Find all occurences of the provided error type in the error's cause chain, * including the error itself. * * @param error The error to get the cause chain from. * @param type The error type. Must be a class definition, such as {@link Error}. */ static findCauses(error: Error, type: AnyErrorConstructor): T[]; /** * Find all occurences of the provided error type in the error's cause chain, * including the error itself. * * @param type The error type. Must be a class definition, such as {@link Error}. */ findCauses(type: ErrorConstructor): T[]; /** * Find the first occurrence of a cause that has the given name * in the error's cause chain, including the error itself. * If not found, `null` will be returned. * * @param error The error to get the cause chain from. * @param name The name of the error to find. */ static findCauseByName(error: Error, name: string): Error | null; /** * Find the first occurrence of a cause that has the given name * in the error's cause chain, including the error itself. * If not found, `null` will be returned. * * @param name The name of the error to find. */ findCauseByName(name: string): Error | null; /** * Find all occurrences of a cause that has the given name * in the error's cause chain, including the error itself. * If not found, `null` will be returned. * * @param error The error to get the cause chain from. * @param name The name of the error to find. */ static findCausesByName(error: Error, name: string): Error[]; /** * Find all occurrences of a cause that has the given name * in the error's cause chain, including the error itself. * If not found, `null` will be returned. * * @param name The name of the error to find. */ findCausesByName(name: string): Error[]; /** * Return the stack trace of the error, including the stack traces * of each cause in the cause chain. * * @param error The error to get the full stack trace for. */ static fullStack(error: Error): string; /** * Return the stack trace of the error, including the stack traces * of each cause in the cause chain. */ fullStack(): string; /** * Convert a given error into a normalised JSON output format. * * @param error The error to convert to JSON. * @param options Output options. */ static toJSON(error: Error, options?: { /** If `true`, includes the stack trace in the output. */ stack?: boolean; /** If `true`, only the top most error will be converted to JSON. The `cause` property will be ommitted. */ shallow?: boolean; }): EErrorJSON; /** * Convert a given error into a normalised JSON output format. * * @param options Output options. */ toJSON(options?: { /** If `true`, includes the stack trace in the output. */ stack?: boolean; /** If `true`, only the top most error will be converted to JSON. The `cause` property will not be included. */ shallow?: boolean; }): EErrorJSON; } /** * An {@link Error} object that has an optional `cause` field. * * This interface is provided to provide compatibiliy with older * versions of ECMAScript (ES2021 and below), as the `cause` field * was only implemented in ES2022. * * @see https://github.com/tc39/proposal-error-cause * * @deprecated Use the built-in {@link Error} interface. */ export interface ErrorLike extends Error { cause?: Error; } /** * Type for a class definition that creates an {@link ErrorLike} instance. * * @deprecated Use the built-in {@link ErrorConstructor} interface. */ export type ErrorLikeConstructor = { new (...args: any[]): T; };