import { ExceptionContext } from './exception-context'; import { ExceptionMessage } from './exception-message'; import { ExceptionTokens } from './exception-tokens'; /** * An {@linkcode Error} subclass that supports tokenized messages and previous errors chaining. * * Message tokens will be inlined into the {@linkcode Error.message}, * but available in the {@linkcode Exception.template} and {@linkcode Exception.tokens}. * * @since v0.2.0 */ export declare class Exception extends Error { /** * Always `Exception`. */ readonly name: string; /** * A template of the error message. * May contain tokens wrapped in double curly braces (i.e. {{ }}). * Token values should be provided in the {@linkcode Exception.tokens} property. * * @example 'Invalid user id {{uid}}' */ readonly template: string; /** * A map of tokens used in the {@linkcode Exception.template} and their (string) values. */ readonly tokens: ExceptionTokens; /** * Additional arbitrary data that may be useful for logging and debugging. */ readonly context: ExceptionContext; /** * A cause of an {@linkcode Exception}. * Can be any {@linkcode Error} or null, if there is no previous error. */ readonly previous: Error | null; constructor(message: ExceptionMessage, context: ExceptionContext, previous: Error | null); /** * Unchains and concatenates all previous errors for user-readable output. */ toString(): string; } /** * Creates an {@linkcode Exception} with a given `message`, `tokens`, and `context`. * The {@linkcode Exception.previous} is null. * * @param message - An error message; may contain tokens wrapped in double curly braces. * @param tokens - A map of tokens listed in the `message` and their string values. * @param context - Additional arbitrary data that may be useful for logging and debugging. * * @since v0.2.0 */ export declare function exception(message: string, tokens?: ExceptionTokens, context?: ExceptionContext): Exception; /** * Creates a function to wrap a previous {@linkcode Error} into an {@linkcode Exception}. * * @param message - An error message; may contain tokens wrapped in double curly braces. * @param tokens - A map of tokens listed in the `message` and their string values. * @param context - Additional arbitrary data that may be useful for logging and debugging. * * @since v0.9.0 */ export declare function chained(message: string, tokens?: ExceptionTokens, context?: ExceptionContext): (previous: Error) => Exception; /** * Creates an {@linkcode Exception} with a given `previous` {@linkcode Error}. * * @param previous - A cause of the {@linkcode Exception}. * @param message - An error message; may contain tokens wrapped in double curly braces. * @param tokens - A map of tokens listed in the `message` and their string values. * @param context - Additional arbitrary data that may be useful for logging and debugging. * * @since v0.2.0 */ export declare function causedBy(previous: Error, message: string, tokens?: ExceptionTokens, context?: ExceptionContext): Exception; /** * Wraps a non-{@linkcode Error} `value` into an {@linkcode Exception}. * The {@linkcode Exception.message} starts with `Caught` * and contains the caught `value` coerced to a string. * * Returns a given {@linkcode Error} `value` as is. * * Since v0.2.1 as `unknownError`. * * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String#string_coercion * * @since v0.10.0 */ export declare function caughtError(value: unknown): Error | Exception; /** * Returns true and narrows the type if a given `value` is an {@linkcode Exception}. * * @since v0.2.0 */ export declare function isException(value: unknown): value is Exception; /** * Returns true and narrows the type if a given `value` is not an {@linkcode Exception}. * * @since v0.2.0 */ export declare function isNotException(value: Exception | T): value is T; /** * Creates an exception with the message about a type mismatch. * * @param name - Name of the value. * @param expected - Excepted value. * @param actual - Actual value. * * @since v0.11.0 */ export declare function typeException(name: string, expected: string, actual: string): Exception; /** * Unwraps all chained errors and returns a user-readable stack of errors. * * @since v0.2.0 */ export declare function chainStack(error: Error): string; /** * Returns the original cause {@linkcode Error} in the error chain. * * @since v0.2.0 */ export declare function fault(error: Error): Error; /** * Unwraps a chain of previous errors and returns them as a list (sorted from most recent error to the original cause). * * @since v0.2.0 */ export declare function unchained(error: Error): Error[];