type ClassConstructor = { new (...args: unknown[]): T; }; export declare enum ErrorCode { VALIDATION_FAILED = 400, UNAUTHORIZED = 401, PAYMENT_REQUIRED = 402, FORBIDDEN = 403, NOT_FOUND = 404, CONFLICT = 409, NO_LONGER_AVAILABLE = 410, EXPIRED = 418, DEFAULT_ERROR = 500, NOT_IMPLEMENTED = 501 } export interface OptionalChainErrorData { message?: string; code?: ErrorCode; key?: Uppercase; payload?: Record; } export declare abstract class ChainError extends Error implements OptionalChainErrorData { /** * Status code, a value from ErrorCode enum. It is directly mapped to HTTP, * status, it is a constant value to be used by clients integrating with * the chain. */ readonly code: ErrorCode; /** * An upper case string to be used as a key do diagnose where the error comes * from and help with regular development. It should not be used by client * integrating with the chain since we don't guarantee it won't change. * It is generated from original error class name. */ readonly key: Uppercase; /** * Additional information to be used by */ readonly payload?: Record; constructor(message: string); constructor(message: string, key: Uppercase); constructor(message: string, key: Uppercase, payload: Record); constructor(message: string, payload: Record | unknown); static normalizedKey(fn: string | Function): Uppercase; static withCode(code: ErrorCode): ClassConstructor; /** * Allows to execute function getting as a parameter the current error. * * @param fn * * @example * throw CommonChainError.objectNotFound(objectId).andExec((e) => { * logger.error(e.message); * }); */ andExec(fn: (e: ChainError) => void): ChainError; logError(logger: { error(message: string): void; }): ChainError; logWarn(logger: { warn(message: string): void; }): ChainError; matches(key: ErrorCode | ClassConstructor): boolean; /** * Maps ChainError to another chain error by error code if `key` param matches * current error code or current diagnostic key. Otherwise, returns original * error. * * Useful in rethrowing an error or mapping an error to another one in catch * clauses or catch methods in promises. * * @param key error code or error class to match * @param newError new error or a function to create the new error */ map(key: ErrorCode | ClassConstructor, newError: ChainError | ((e: ChainError) => ChainError)): ChainError; static isChainError(e: object | undefined): e is ChainError; static from(e: object & { message?: string; }): ChainError; static matches(e: { message?: string; } | ChainError, key: ErrorCode | ClassConstructor): boolean; /** * Maps ChainError to another chain error by error code, or returns original * error if no error code matches, or returns default chain error if a given * parameter is not a ChainError instance. * * Useful in rethrowing an error or mapping an error to another one in catch * clauses or catch methods in promises. * * @param e original error * @param key error code or error class to match * @param newError new error or a function to create the new error */ static map(e: { message?: string; } | ChainError, key: ErrorCode | ClassConstructor, newError: ChainError | ((e: ChainError) => ChainError)): ChainError; /** * Recovers (mutes) ChainError to a specified return value or undefined. * * If the error is a ChainError and matches the error code, the error is * recovered and the specified return value is returned. Otherwise, the error * is re-thrown. * * For instance when you want to get an object from chain, and instead of * throwing a NOT_FOUND error, you want to return undefined: * * ```ts * getObjectByKey(...) * .catch((e) => CommonChainError.recover(e, ErrorCode.NOT_FOUND)); * ``` * * If you want to return a default value instead of undefined, you can do: * * ```ts * getObjectByKey(...) * .catch((e) => CommonChainError.recover(e, ErrorCode.NOT_FOUND, defaultValue)); * ``` * * @param e original error * @param key error code or error class to match * @param defaultValue value to be returned if error code matches */ static recover(e: { message?: string; } | ChainError, key: ErrorCode | ClassConstructor, defaultValue?: T): T; /** * Recovers (mutes) ChainError to a specified return value or undefined. * * @deprecated Use {@link ChainError.recover} instead. */ static ignore(e: { message?: string; } | ChainError, key: ErrorCode | ClassConstructor, defaultValue?: T): T; } declare const ValidationFailedError_base: ClassConstructor; export declare class ValidationFailedError extends ValidationFailedError_base { } declare const UnauthorizedError_base: ClassConstructor; export declare class UnauthorizedError extends UnauthorizedError_base { } declare const PaymentRequiredError_base: ClassConstructor; export declare class PaymentRequiredError extends PaymentRequiredError_base { } declare const ForbiddenError_base: ClassConstructor; export declare class ForbiddenError extends ForbiddenError_base { } declare const NotFoundError_base: ClassConstructor; export declare class NotFoundError extends NotFoundError_base { } declare const ConflictError_base: ClassConstructor; export declare class ConflictError extends ConflictError_base { } declare const NoLongerAvailableError_base: ClassConstructor; export declare class NoLongerAvailableError extends NoLongerAvailableError_base { } declare const DefaultError_base: ClassConstructor; export declare class DefaultError extends DefaultError_base { } declare const ExpiredError_base: ClassConstructor; export declare class ExpiredError extends ExpiredError_base { } declare const RuntimeError_base: ClassConstructor; export declare class RuntimeError extends RuntimeError_base { } declare const NotImplementedError_base: ClassConstructor; export declare class NotImplementedError extends NotImplementedError_base { } export {}; //# sourceMappingURL=error.d.ts.map