UNPKG

nani

Version:

Better error handling for Node

118 lines (117 loc) 4.21 kB
import { ErrorOptions } from "./error-options"; /** * A base class to use as a replacement for the standard Error class. * * @remarks * Your custom error types should inherit from this class in order to make use * of its constructor pattern and fullName resolution capabilities. */ export declare class NaniError extends Error { /** * A string to prepend to error messages. * * @remarks * The prefix will be separated from the rest of the message with spaces and * a colon, the same way causes are separated in a cause chain. * * Seting this will affect messages for all errors of the relevant type, * both in their own message properties and within the cause chanis of * other errors. It will *not* affect shortMessage properties, however. */ static prefix?: string; /** * Cached value of the fullName property to prevent recalcuation. */ private static _fullName; /** * The error message without any chained cause messages. */ shortMessage: string; /** * The cause of the error, for cause chains. */ cause: Error | null; /** * Additional arbitary data about the error. */ info: Record<string, any> | null; /** * True if the constructor was not provided with a short message. False * otherwise. Useful for unit testing of thrown errors. */ usedDefaultMessage: boolean; /** * Constructs a NaniError. * @param options - Error options. */ constructor(options?: ErrorOptions); /** * Constructs a NaniError. * @param shortMessageOrCause - Either the short message or the cause. * @param options - Additional error options. */ constructor(shortMessageOrCause: string | Error, options?: ErrorOptions); /** * Constructs a NaniError. * @param shortMessage - The error's short message. * @param cause - The error's cause. * @param options - Additional error options. */ constructor(shortMessage: string, cause: Error, options?: ErrorOptions); /** * The dot-separated full name of the error constructor. * * @remarks * This is a read-only property and should not be overridden, as it is * critical to nani's mechanism for checking against error name heirarchies. */ static get fullName(): string; /** * Fetches the shortMessage which will be used for any instance that is not * provided with one. * * @remarks * By default, this returns a generic message, but it may be overridden to * provide custom default messages for your subclasses. * * @param info - The value of `options.info` provided to the constructor, * if any, or an empty object otherwise. Allows you to easily include * values from `options.info` in your default messages. * @returns The default shortMessage. */ static getDefaultMessage(info: Record<string, any>): string; /** * Internal method that actually determines the fullName of the constructor. * * @remarks * This should only be called once for each class, as the result will be * stored on the class itself as part of lazy evaluation of the fullName * property. */ private static _getFullName; /** * Internal method for normalizing NaniError constructor arguments into * a single options object. * @param args - Constructor arguments. * @returns Constructor arguments as if they were all provided in a plain * options object. */ private static _normalizeArgs; /** * The name of the error, equivalent to the constructor name. * * @remarks * This is a read-only property and should not be overridden, as it is * critical to nani's mechanism for checking against error name heirarchies. */ get name(): string; /** * The dot-separated full name of the error, equivalent to that of the * constructor. * * @remarks * This is a read-only property and should not be overridden, as it is * critical to Nani's mechanism for checking against error name heirarchies. */ get fullName(): string; }