UNPKG

http-error-kit

Version:

A flexible and customizable error-handling library for HTTP applications. Provides structured error responses with optional formatters, predefined HTTP errors, and extensible error types.

647 lines (646 loc) 29.9 kB
import { IFormatter } from "./types/formatter.type"; import { IRawInput } from "./interfaces/input.interface"; /** * Represents a customizable error with a status code, message, and optional details. * @extends {Error} */ export declare class KitHttpError extends Error { /** * Allows for dynamic assignment of arbitrary properties. * @type {unknown} */ [key: string]: unknown; /** * The HTTP status code associated with the error. * @type {number} */ statusCode?: number; /** * A human-readable error message. * @type {string} */ message: string; /** * Additional details about the error, if any. * @type {*} */ details?: unknown; /** * A default formatter function used to format error details when no instance-specific formatter is provided. * @type {(statusCode: number, message: string, details?: unknown, ...args: unknown[]) => unknown} * @private * @static */ private static defaultFormatter?; /** * An instance-specific formatter function to override the global formatter. * @private * @type {(statusCode: number, message: string, details?: unknown, ...args: unknown[]) => unknown} */ private instanceFormatter?; /** * The raw input data associated with this instance. * @private * @type {IRawInput} */ private rawInputs; /** * Initializes a new instance of the KitHttpError class. * @param {number} statusCode - The HTTP status code associated with the error. * @param {string} message - A human-readable error message. * @param {*} [details] - Additional details about the error, if any. * @param {...any} args - Additional arguments to pass to the formatter function. */ constructor(statusCode: number, message: string, details?: unknown, ...args: unknown[]); /** * Configures a global formatter function for all instances of KitHttpError. * This formatter will be used to format error details unless an instance-specific formatter is provided. * @param {(statusCode: number, message: string, details?: unknown, ...args: unknown[]) => unknown} formatter - A function to format error details, which takes the status code, message, optional details, and additional arguments as parameters. */ static _configureFormatter(formatter: IFormatter): void; /** * Sets a custom formatter function for this instance, which will be used to format error details. * This will override any global formatter function set by `KitHttpErrorConfig.configureFormatter()`. * @param {IFormatter} formatter - A function to format error details, which takes the status code, message, optional details, and additional arguments as parameters. * @returns {this} The current instance for method chaining. */ setFormatter(formatter: IFormatter): this; /** * Returns the raw input data associated with this instance. * @returns {IRawInput} The raw input data associated with this instance. */ getInputs(): IRawInput; /** * Returns a JSON-compatible object representation of this instance. * If a formatter function is provided (either globally or instance-specific), * it will be used to format the error details. * Otherwise, the object will contain the `statusCode`, `message`, and `details` properties. * @returns {{statusCode: number, message: string, details: unknown}} A JSON-compatible object representation of this instance. */ toJSON(): unknown; } /** * Configuration class for KitHttpError. * Provides methods to configure the formatter function used by KitHttpError instances. */ export declare class KitHttpErrorConfig { /** * Configures a global formatter function for all instances of KitHttpError. * This formatter will be used to format error details unless an instance-specific formatter is provided. * When this method is called, KitHttpError-based errors will be used globally. * @param {(statusCode: number, message: string, details?: unknown, ...args: unknown[]) => unknown} formatter - A function to format error details, which takes the status code, message, optional details, and additional arguments as parameters. */ static configureFormatter(formatter: IFormatter): void; /** * Checks if KitHttpError-based errors are being used globally. * * @returns {boolean} True if KitHttpError-based errors are enabled globally, false otherwise. */ static shouldUseKitHttpErrors(): boolean; } /** * Creates a new instance of the KitHttpError class with a status code of 400, Bad Request. * @extends KitHttpError */ export declare class BadRequestError extends KitHttpError { /** * Creates a new instance of the BadRequestError class. * @param {string} message - The error message. Defaults to "Bad Request" if not provided. * @param {any} [details] - Additional error details. * @param {...any} args - Additional arguments to pass to the formatter function. */ constructor(message?: string, details?: unknown, ...args: unknown[]); } /** * Creates a new instance of the KitHttpError class with a status code of 401, Unauthorized. * @extends KitHttpError */ export declare class UnauthorizedError extends KitHttpError { /** * Creates a new instance of the UnauthorizedError class. * @param {string} [message] - The error message. Defaults to "Unauthorized" if not provided. * @param {any} [details] - Additional error details. * @param {...any} args - Additional arguments to pass to the formatter function. */ constructor(message?: string, details?: unknown, ...args: unknown[]); } /** * Creates a new instance of the KitHttpError class with a status code of 402, Payment Required. * @extends KitHttpError */ export declare class PaymentRequiredError extends KitHttpError { /** * Creates a new instance of the PaymentRequiredError class. * @param {string} [message] - The error message. Defaults to "Payment Required" if not provided. * @param {any} [details] - Additional error details. * @param {...any} args - Additional arguments to pass to the formatter function. */ constructor(message?: string, details?: unknown, ...args: unknown[]); } /** * Creates a new instance of the KitHttpError class with a status code of 403, Forbidden. * @extends KitHttpError */ export declare class ForbiddenError extends KitHttpError { /** * Creates a new instance of the ForbiddenError class. * @param {string} [message] - The error message. Defaults to "Forbidden" if not provided. * @param {any} [details] - Additional error details. * @param {...any} args - Additional arguments to pass to the formatter function. */ constructor(message?: string, details?: unknown, ...args: unknown[]); } /** * Creates a new instance of the KitHttpError class with a status code of 404, Not Found. * @extends KitHttpError */ export declare class NotFoundError extends KitHttpError { /** * Creates a new instance of the NotFoundError class. * @param {string} [message] - The error message. Defaults to "Not Found" if not provided. * @param {any} [details] - Additional error details. * @param {...any} args - Additional arguments to pass to the formatter function. */ constructor(message?: string, details?: unknown, ...args: unknown[]); } /** * Creates a new instance of the KitHttpError class with a status code of 405, Method Not Allowed. * @extends KitHttpError */ export declare class MethodNotAllowedError extends KitHttpError { /** * Creates a new instance of the MethodNotAllowedError class. * @param {string} [message] - The error message. Defaults to "Method Not Allowed" if not provided. * @param {any} [details] - Additional error details. * @param {...any} args - Additional arguments to pass to the formatter function. */ constructor(message?: string, details?: unknown, ...args: unknown[]); } /** * Creates a new instance of the KitHttpError class with a status code of 406, Not Acceptable. * @extends KitHttpError */ export declare class NotAcceptableError extends KitHttpError { /** * Creates a new instance of the NotAcceptableError class. * @param {string} [message] - The error message. Defaults to "Not Acceptable" if not provided. * @param {any} [details] - Additional error details. * @param {...any} args - Additional arguments to pass to the formatter function. */ constructor(message?: string, details?: unknown, ...args: unknown[]); } /** * Creates a new instance of the KitHttpError class with a status code of 407, Proxy Authentication Required. * @extends KitHttpError */ export declare class ProxyAuthenticationRequiredError extends KitHttpError { /** * Creates a new instance of the ProxyAuthenticationRequiredError class. * @param {string} [message] - The error message. Defaults to "Proxy Authentication Required" if not provided. * @param {any} [details] - Additional error details. * @param {...any} args - Additional arguments to pass to the formatter function. */ constructor(message?: string, details?: unknown, ...args: unknown[]); } /** * Creates a new instance of the KitHttpError class with a status code of 408, Request Timeout. * @extends KitHttpError */ export declare class RequestTimeoutError extends KitHttpError { /** * Creates a new instance of the RequestTimeoutError class. * @param {string} [message] - The error message. Defaults to "Request Timeout" if not provided. * @param {any} [details] - Additional error details. * @param {...any} args - Additional arguments to pass to the formatter function. */ constructor(message?: string, details?: unknown, ...args: unknown[]); } /** * Creates a new instance of the KitHttpError class with a status code of 409, Conflict. * @extends KitHttpError */ export declare class ConflictError extends KitHttpError { /** * Creates a new instance of the ConflictError class. * @param {string} [message] - The error message. Defaults to "Conflict" if not provided. * @param {any} [details] - Additional error details. * @param {...any} args - Additional arguments to pass to the formatter function. */ constructor(message?: string, details?: unknown, ...args: unknown[]); } /** * Creates a new instance of the KitHttpError class with a status code of 410, Gone. * @extends KitHttpError */ export declare class GoneError extends KitHttpError { /** * Creates a new instance of the GoneError class. * @param {string} [message] - The error message. Defaults to "Gone" if not provided. * @param {any} [details] - Additional error details. * @param {...any} args - Additional arguments to pass to the formatter function. */ constructor(message?: string, details?: unknown, ...args: unknown[]); } /** * Creates a new instance of the KitHttpError class with a status code of 411, Length Required. * @extends KitHttpError */ export declare class LengthRequiredError extends KitHttpError { /** * Creates a new instance of the LengthRequiredError class. * @param {string} [message] - The error message. Defaults to "Length Required" if not provided. * @param {any} [details] - Additional error details. * @param {...any} args - Additional arguments to pass to the formatter function. */ constructor(message?: string, details?: unknown, ...args: unknown[]); } /** * Creates a new instance of the KitHttpError class with a status code of 412, Precondition Failed. * @extends KitHttpError */ export declare class PreconditionFailedError extends KitHttpError { /** * Creates a new instance of the PreconditionFailedError class. * @param {string} [message] - The error message. Defaults to "Precondition Failed" if not provided. * @param {any} [details] - Additional error details. * @param {...any} args - Additional arguments to pass to the formatter function. */ constructor(message?: string, details?: unknown, ...args: unknown[]); } /** * Creates a new instance of the KitHttpError class with a status code of 413, Request Entity Too Large. * @extends KitHttpError */ export declare class RequestTooLongError extends KitHttpError { /** * Creates a new instance of the RequestTooLongError class. * @param {string} [message] - The error message. Defaults to "Request Entity Too Large" if not provided. * @param {any} [details] - Additional error details. * @param {...any} args - Additional arguments to pass to the formatter function. */ constructor(message?: string, details?: unknown, ...args: unknown[]); } /** * Creates a new instance of the KitHttpError class with a status code of 414, Request-URI Too Long. * @extends KitHttpError */ export declare class RequestUriTooLongError extends KitHttpError { /** * Creates a new instance of the RequestUriTooLongError class. * @param {string} [message] - The error message. Defaults to "Request-URI Too Long" if not provided. * @param {any} [details] - Additional error details. * @param {...any} args - Additional arguments to pass to the formatter function. */ constructor(message?: string, details?: unknown, ...args: unknown[]); } /** * Creates a new instance of the KitHttpError class with a status code of 415, Unsupported Media Type. * @extends KitHttpError */ export declare class UnsupportedMediaTypeError extends KitHttpError { /** * Creates a new instance of the UnsupportedMediaTypeError class. * @param {string} [message] - The error message. Defaults to "Unsupported Media Type" if not provided. * @param {any} [details] - Additional error details. * @param {...any} args - Additional arguments to pass to the formatter function. */ constructor(message?: string, details?: unknown, ...args: unknown[]); } /** * Creates a new instance of the KitHttpError class with a status code of 416, Requested Range Not Satisfiable. * @extends KitHttpError */ export declare class RequestedRangeNotSatisfiableError extends KitHttpError { /** * Creates a new instance of the RequestedRangeNotSatisfiableError class. * @param {string} [message] - The error message. Defaults to "Requested Range Not Satisfiable" if not provided. * @param {any} [details] - Additional error details. * @param {...any} args - Additional arguments to pass to the formatter function. */ constructor(message?: string, details?: unknown, ...args: unknown[]); } /** * Creates a new instance of the KitHttpError class with a status code of 417, Expectation Failed. * @extends KitHttpError */ export declare class ExpectationFailedError extends KitHttpError { /** * Creates a new instance of the ExpectationFailedError class. * @param {string} [message] - The error message. Defaults to "Expectation Failed" if not provided. * @param {any} [details] - Additional error details. * @param {...any} args - Additional arguments to pass to the formatter function. */ constructor(message?: string, details?: unknown, ...args: unknown[]); } /** * Creates a new instance of the KitHttpError class with a status code of 418, I'm a teapot. * @extends KitHttpError */ export declare class ImATeapotError extends KitHttpError { /** * Creates a new instance of the ImATeapotError class. * @param {string} [message] - The error message. Defaults to "I'm a teapot" if not provided. * @param {any} [details] - Additional error details. * @param {...any} args - Additional arguments to pass to the formatter function. */ constructor(message?: string, details?: unknown, ...args: unknown[]); } /** * Creates a new instance of the KitHttpError class with a status code of 419, Insufficient Space on Resource. * @extends KitHttpError */ export declare class InsufficientSpaceOnResourceError extends KitHttpError { /** * Creates a new instance of the InsufficientSpaceOnResourceError class. * @param {string} [message] - The error message. Defaults to "Insufficient Space on Resource" if not provided. * @param {any} [details] - Additional error details. * @param {...any} args - Additional arguments to pass to the formatter function. */ constructor(message?: string, details?: unknown, ...args: unknown[]); } /** * Creates a new instance of the KitHttpError class with a status code of 420, Method Failure. * @extends KitHttpError */ export declare class MethodFailureError extends KitHttpError { /** * Creates a new instance of the MethodFailureError class. * @param {string} [message] - The error message. Defaults to "Method Failure" if not provided. * @param {any} [details] - Additional error details. * @param {...any} args - Additional arguments to pass to the formatter function. */ constructor(message?: string, details?: unknown, ...args: unknown[]); } /** * Creates a new instance of the KitHttpError class with a status code of 421, Misdirected Request. * @extends KitHttpError */ export declare class MisdirectedRequestError extends KitHttpError { /** * Creates a new instance of the MisdirectedRequestError class. * @param {string} [message] - The error message. Defaults to "Misdirected Request" if not provided. * @param {any} [details] - Additional error details. * @param {...any} args - Additional arguments to pass to the formatter function. */ constructor(message?: string, details?: unknown, ...args: unknown[]); } /** * Creates a new instance of the KitHttpError class with a status code of 422, Unprocessable Entity. * @extends KitHttpError */ export declare class UnprocessableEntityError extends KitHttpError { /** * Creates a new instance of the UnprocessableEntityError class. * @param {string} [message] - The error message. Defaults to "Unprocessable Entity" if not provided. * @param {any} [details] - Additional error details. * @param {...any} args - Additional arguments to pass to the formatter function. */ constructor(message?: string, details?: unknown, ...args: unknown[]); } /** * Creates a new instance of the KitHttpError class with a status code of 423, Locked. * @extends KitHttpError */ export declare class LockedError extends KitHttpError { /** * Creates a new instance of the LockedError class. * @param {string} [message] - The error message. Defaults to "Locked" if not provided. * @param {any} [details] - Additional error details. * @param {...any} args - Additional arguments to pass to the formatter function. */ constructor(message?: string, details?: unknown, ...args: unknown[]); } /** * Creates a new instance of the KitHttpError class with a status code of 424, Failed Dependency. * @extends KitHttpError */ export declare class FailedDependencyError extends KitHttpError { /** * Creates a new instance of the FailedDependencyError class. * @param {string} [message] - The error message. Defaults to "Failed Dependency" if not provided. * @param {any} [details] - Additional error details. * @param {...any} args - Additional arguments to pass to the formatter function. */ constructor(message?: string, details?: unknown, ...args: unknown[]); } /** * Creates a new instance of the KitHttpError class with a status code of 425, Too Early. * @extends KitHttpError */ export declare class TooEarlyError extends KitHttpError { /** * Creates a new instance of the TooEarlyError class. * @param {string} [message] - The error message. Defaults to "Too Early" if not provided. * @param {any} [details] - Additional error details. * @param {...any} args - Additional arguments to pass to the formatter function. */ constructor(message?: string, details?: unknown, ...args: unknown[]); } /** * Creates a new instance of the KitHttpError class with a status code of 426, Upgrade Required. * @extends KitHttpError */ export declare class UpgradeRequiredError extends KitHttpError { /** * Creates a new instance of the UpgradeRequiredError class. * @param {string} [message] - The error message. Defaults to "Upgrade Required" if not provided. * @param {any} [details] - Additional error details. * @param {...any} args - Additional arguments to pass to the formatter function. */ constructor(message?: string, details?: unknown, ...args: unknown[]); } /** * Creates a new instance of the KitHttpError class with a status code of 428, Precondition Required. * @extends KitHttpError */ export declare class PreconditionRequiredError extends KitHttpError { /** * Creates a new instance of the PreconditionRequiredError class. * @param {string} [message] - The error message. Defaults to "Precondition Required" if not provided. * @param {any} [details] - Additional error details. * @param {...any} args - Additional arguments to pass to the formatter function. */ constructor(message?: string, details?: unknown, ...args: unknown[]); } /** * Creates a new instance of the KitHttpError class with a status code of 429, Too Many Requests. * @extends KitHttpError */ export declare class TooManyRequestsError extends KitHttpError { /** * Creates a new instance of the TooManyRequestsError class. * @param {string} [message] - The error message. Defaults to "Too Many Requests" if not provided. * @param {any} [details] - Additional error details. * @param {...any} args - Additional arguments to pass to the formatter function. */ constructor(message?: string, details?: unknown, ...args: unknown[]); } /** * Creates a new instance of the KitHttpError class with a status code of 431, Request Header Fields Too Large. * @extends KitHttpError */ export declare class RequestHeaderFieldsTooLargeError extends KitHttpError { /** * Creates a new instance of the RequestHeaderFieldsTooLargeError class. * @param {string} [message] - The error message. Defaults to "Request Header Fields Too Large" if not provided. * @param {any} [details] - Additional error details. * @param {...any} args - Additional arguments to pass to the formatter function. */ constructor(message?: string, details?: unknown, ...args: unknown[]); } /** * Creates a new instance of the KitHttpError class with a status code of 451, Unavailable For Legal Reasons. * @extends KitHttpError */ export declare class UnavailableForLegalReasonsError extends KitHttpError { /** * Creates a new instance of the UnavailableForLegalReasonsError class. * @param {string} [message] - The error message. Defaults to "Unavailable For Legal Reasons" if not provided. * @param {any} [details] - Additional error details. * @param {...any} args - Additional arguments to pass to the formatter function. */ constructor(message?: string, details?: unknown, ...args: unknown[]); } /** * Creates a new instance of the KitHttpError class with a status code of 500, Internal Server Error. * @extends KitHttpError */ export declare class InternalServerError extends KitHttpError { /** * Creates a new instance of the InternalServerError class. * @param {string} [message] - The error message. Defaults to "Internal Server Error" if not provided. * @param {any} [details] - Additional error details. * @param {...any} args - Additional arguments to pass to the formatter function. */ constructor(message?: string, details?: unknown, ...args: unknown[]); } /** * Creates a new instance of the KitHttpError class with a status code of 501, Not Implemented. * @extends KitHttpError */ export declare class NotImplementedError extends KitHttpError { /** * Creates a new instance of the NotImplementedError class. * @param {string} [message] - The error message. Defaults to "Not Implemented" if not provided. * @param {any} [details] - Additional error details. * @param {...any} args - Additional arguments to pass to the formatter function. */ constructor(message?: string, details?: unknown, ...args: unknown[]); } /** * Creates a new instance of the KitHttpError class with a status code of 502, Bad Gateway. * @extends KitHttpError */ export declare class BadGatewayError extends KitHttpError { /** * Creates a new instance of the BadGatewayError class. * @param {string} [message] - The error message. Defaults to "Bad Gateway" if not provided. * @param {any} [details] - Additional error details. * @param {...any} args - Additional arguments to pass to the formatter function. */ constructor(message?: string, details?: unknown, ...args: unknown[]); } /** * Creates a new instance of the KitHttpError class with a status code of 503, Service Unavailable. * @extends KitHttpError */ export declare class ServiceUnavailableError extends KitHttpError { /** * Creates a new instance of the ServiceUnavailableError class. * @param {string} [message] - The error message. Defaults to "Service Unavailable" if not provided. * @param {any} [details] - Additional error details. * @param {...any} args - Additional arguments to pass to the formatter function. */ constructor(message?: string, details?: unknown, ...args: unknown[]); } /** * Creates a new instance of the KitHttpError class with a status code of 504, Gateway Timeout. * @extends KitHttpError */ export declare class GatewayTimeoutError extends KitHttpError { /** * Creates a new instance of the GatewayTimeoutError class. * @param {string} [message] - The error message. Defaults to "Gateway Timeout" if not provided. * @param {any} [details] - Additional error details. * @param {...any} args - Additional arguments to pass to the formatter function. */ constructor(message?: string, details?: unknown, ...args: unknown[]); } /** * Creates a new instance of the KitHttpError class with a status code of 505, HTTP Version Not Supported. * @extends KitHttpError */ export declare class HttpVersionNotSupportedError extends KitHttpError { /** * Creates a new instance of the HttpVersionNotSupportedError class. * @param {string} [message] - The error message. Defaults to "HTTP Version Not Supported" if not provided. * @param {any} [details] - Additional error details. * @param {...any} args - Additional arguments to pass to the formatter function. */ constructor(message?: string, details?: unknown, ...args: unknown[]); } /** * Creates a new instance of the KitHttpError class with a status code of 506, Variant Also Negotiates. * @extends KitHttpError */ export declare class VariantAlsoNegotiatesError extends KitHttpError { /** * Creates a new instance of the VariantAlsoNegotiatesError class. * @param {string} [message] - The error message. Defaults to "Variant Also Negotiates" if not provided. * @param {any} [details] - Additional error details. * @param {...any} args - Additional arguments to pass to the formatter function. */ constructor(message?: string, details?: unknown, ...args: unknown[]); } /** * Creates a new instance of the KitHttpError class with a status code of 507, Insufficient Storage. * @extends KitHttpError */ export declare class InsufficientStorageError extends KitHttpError { /** * Creates a new instance of the InsufficientStorageError class. * @param {string} [message] - The error message. Defaults to "Insufficient Storage" if not provided. * @param {any} [details] - Additional error details. * @param {...any} args - Additional arguments to pass to the formatter function. */ constructor(message?: string, details?: unknown, ...args: unknown[]); } /** * Creates a new instance of the KitHttpError class with a status code of 508, Loop Detected. * @extends KitHttpError */ export declare class LoopDetectedError extends KitHttpError { /** * Creates a new instance of the LoopDetectedError class. * @param {string} [message] - The error message. Defaults to "Loop Detected" if not provided. * @param {any} [details] - Additional error details. * @param {...any} args - Additional arguments to pass to the formatter function. */ constructor(message?: string, details?: unknown, ...args: unknown[]); } /** * Creates a new instance of the KitHttpError class with a status code of 510, Not Extended. * @extends KitHttpError */ export declare class NotExtendedError extends KitHttpError { /** * Creates a new instance of the NotExtendedError class. * @param {string} [message] - The error message. Defaults to "Not Extended" if not provided. * @param {any} [details] - Additional error details. * @param {...any} args - Additional arguments to pass to the formatter function. */ constructor(message?: string, details?: unknown, ...args: unknown[]); } /** * Creates a new instance of the KitHttpError class with a status code of 511, Network Authentication Required. * @extends KitHttpError */ export declare class NetworkAuthenticationRequiredError extends KitHttpError { /** * Creates a new instance of the NetworkAuthenticationRequiredError class. * @param {string} [message] - The error message. Defaults to "Network Authentication Required" if not provided. * @param {any} [details] - Additional error details. * @param {...any} args - Additional arguments to pass to the formatter function. */ constructor(message?: string, details?: unknown, ...args: unknown[]); }