http-errors-plus
Version:
Next-gen HTTP error handling for modern JavaScript and TypeScript apps — structured, overrideable, and developer-focused.
178 lines (177 loc) • 6.29 kB
TypeScript
/**
* @typedef {Object} StatusEntry
* @property {number} code
* @property {string} name
* @property {string} message
*/
/**
* Maps HTTP status code to name/message
* @type {Record<number, StatusEntry>}
*/
export const STATUS_CODE_MAP: Record<number, StatusEntry>;
/**
* Maps HTTP status name to code
* @type {Record<string, StatusEntry>}
*/
export const STATUS_NAME_MAP: Record<string, StatusEntry>;
/**
* @typedef {Object} HttpErrorConstant
* @property {number} status - HTTP status code
* @property {string} code - Uppercase constant name (e.g., NOT_FOUND)
* @property {string} message - Human-readable HTTP message
*/
/**
* @typedef {{
* CONTINUE: HttpErrorConstant,
* SWITCHING_PROTOCOLS: HttpErrorConstant,
* PROCESSING: HttpErrorConstant,
* EARLY_HINTS: HttpErrorConstant,
* OK: HttpErrorConstant,
* CREATED: HttpErrorConstant,
* ACCEPTED: HttpErrorConstant,
* NON_AUTHORITATIVE_INFORMATION: HttpErrorConstant,
* NO_CONTENT: HttpErrorConstant,
* RESET_CONTENT: HttpErrorConstant,
* PARTIAL_CONTENT: HttpErrorConstant,
* MULTI_STATUS: HttpErrorConstant,
* ALREADY_REPORTED: HttpErrorConstant,
* IM_USED: HttpErrorConstant,
* MULTIPLE_CHOICES: HttpErrorConstant,
* MOVED_PERMANENTLY: HttpErrorConstant,
* FOUND: HttpErrorConstant,
* SEE_OTHER: HttpErrorConstant,
* NOT_MODIFIED: HttpErrorConstant,
* USE_PROXY: HttpErrorConstant,
* TEMPORARY_REDIRECT: HttpErrorConstant,
* PERMANENT_REDIRECT: HttpErrorConstant,
* BAD_REQUEST: HttpErrorConstant,
* UNAUTHORIZED: HttpErrorConstant,
* PAYMENT_REQUIRED: HttpErrorConstant,
* FORBIDDEN: HttpErrorConstant,
* NOT_FOUND: HttpErrorConstant,
* METHOD_NOT_ALLOWED: HttpErrorConstant,
* NOT_ACCEPTABLE: HttpErrorConstant,
* PROXY_AUTHENTICATION_REQUIRED: HttpErrorConstant,
* REQUEST_TIMEOUT: HttpErrorConstant,
* CONFLICT: HttpErrorConstant,
* GONE: HttpErrorConstant,
* LENGTH_REQUIRED: HttpErrorConstant,
* PRECONDITION_FAILED: HttpErrorConstant,
* PAYLOAD_TOO_LARGE: HttpErrorConstant,
* URI_TOO_LONG: HttpErrorConstant,
* UNSUPPORTED_MEDIA_TYPE: HttpErrorConstant,
* RANGE_NOT_SATISFIABLE: HttpErrorConstant,
* EXPECTATION_FAILED: HttpErrorConstant,
* I_M_A_TEAPOT: HttpErrorConstant,
* MISDIRECTED_REQUEST: HttpErrorConstant,
* UNPROCESSABLE_ENTITY: HttpErrorConstant,
* LOCKED: HttpErrorConstant,
* FAILED_DEPENDENCY: HttpErrorConstant,
* TOO_EARLY: HttpErrorConstant,
* UPGRADE_REQUIRED: HttpErrorConstant,
* PRECONDITION_REQUIRED: HttpErrorConstant,
* TOO_MANY_REQUESTS: HttpErrorConstant,
* REQUEST_HEADER_FIELDS_TOO_LARGE: HttpErrorConstant,
* UNAVAILABLE_FOR_LEGAL_REASONS: HttpErrorConstant,
* INTERNAL_SERVER_ERROR: HttpErrorConstant,
* NOT_IMPLEMENTED: HttpErrorConstant,
* BAD_GATEWAY: HttpErrorConstant,
* SERVICE_UNAVAILABLE: HttpErrorConstant,
* GATEWAY_TIMEOUT: HttpErrorConstant,
* HTTP_VERSION_NOT_SUPPORTED: HttpErrorConstant,
* VARIANT_ALSO_NEGOTIATES: HttpErrorConstant,
* INSUFFICIENT_STORAGE: HttpErrorConstant,
* LOOP_DETECTED: HttpErrorConstant,
* BANDWIDTH_LIMIT_EXCEEDED: HttpErrorConstant,
* NOT_EXTENDED: HttpErrorConstant,
* NETWORK_AUTHENTICATION_REQUIRED: HttpErrorConstant,
* }} BaseHttpErrors
*/
/**
* @type {BaseHttpErrors}
*/
export const BASE_HTTP_ERRORS: BaseHttpErrors;
export type StatusEntry = {
code: number;
name: string;
message: string;
};
export type HttpErrorConstant = {
/**
* - HTTP status code
*/
status: number;
/**
* - Uppercase constant name (e.g., NOT_FOUND)
*/
code: string;
/**
* - Human-readable HTTP message
*/
message: string;
};
export type BaseHttpErrors = {
CONTINUE: HttpErrorConstant;
SWITCHING_PROTOCOLS: HttpErrorConstant;
PROCESSING: HttpErrorConstant;
EARLY_HINTS: HttpErrorConstant;
OK: HttpErrorConstant;
CREATED: HttpErrorConstant;
ACCEPTED: HttpErrorConstant;
NON_AUTHORITATIVE_INFORMATION: HttpErrorConstant;
NO_CONTENT: HttpErrorConstant;
RESET_CONTENT: HttpErrorConstant;
PARTIAL_CONTENT: HttpErrorConstant;
MULTI_STATUS: HttpErrorConstant;
ALREADY_REPORTED: HttpErrorConstant;
IM_USED: HttpErrorConstant;
MULTIPLE_CHOICES: HttpErrorConstant;
MOVED_PERMANENTLY: HttpErrorConstant;
FOUND: HttpErrorConstant;
SEE_OTHER: HttpErrorConstant;
NOT_MODIFIED: HttpErrorConstant;
USE_PROXY: HttpErrorConstant;
TEMPORARY_REDIRECT: HttpErrorConstant;
PERMANENT_REDIRECT: HttpErrorConstant;
BAD_REQUEST: HttpErrorConstant;
UNAUTHORIZED: HttpErrorConstant;
PAYMENT_REQUIRED: HttpErrorConstant;
FORBIDDEN: HttpErrorConstant;
NOT_FOUND: HttpErrorConstant;
METHOD_NOT_ALLOWED: HttpErrorConstant;
NOT_ACCEPTABLE: HttpErrorConstant;
PROXY_AUTHENTICATION_REQUIRED: HttpErrorConstant;
REQUEST_TIMEOUT: HttpErrorConstant;
CONFLICT: HttpErrorConstant;
GONE: HttpErrorConstant;
LENGTH_REQUIRED: HttpErrorConstant;
PRECONDITION_FAILED: HttpErrorConstant;
PAYLOAD_TOO_LARGE: HttpErrorConstant;
URI_TOO_LONG: HttpErrorConstant;
UNSUPPORTED_MEDIA_TYPE: HttpErrorConstant;
RANGE_NOT_SATISFIABLE: HttpErrorConstant;
EXPECTATION_FAILED: HttpErrorConstant;
I_M_A_TEAPOT: HttpErrorConstant;
MISDIRECTED_REQUEST: HttpErrorConstant;
UNPROCESSABLE_ENTITY: HttpErrorConstant;
LOCKED: HttpErrorConstant;
FAILED_DEPENDENCY: HttpErrorConstant;
TOO_EARLY: HttpErrorConstant;
UPGRADE_REQUIRED: HttpErrorConstant;
PRECONDITION_REQUIRED: HttpErrorConstant;
TOO_MANY_REQUESTS: HttpErrorConstant;
REQUEST_HEADER_FIELDS_TOO_LARGE: HttpErrorConstant;
UNAVAILABLE_FOR_LEGAL_REASONS: HttpErrorConstant;
INTERNAL_SERVER_ERROR: HttpErrorConstant;
NOT_IMPLEMENTED: HttpErrorConstant;
BAD_GATEWAY: HttpErrorConstant;
SERVICE_UNAVAILABLE: HttpErrorConstant;
GATEWAY_TIMEOUT: HttpErrorConstant;
HTTP_VERSION_NOT_SUPPORTED: HttpErrorConstant;
VARIANT_ALSO_NEGOTIATES: HttpErrorConstant;
INSUFFICIENT_STORAGE: HttpErrorConstant;
LOOP_DETECTED: HttpErrorConstant;
BANDWIDTH_LIMIT_EXCEEDED: HttpErrorConstant;
NOT_EXTENDED: HttpErrorConstant;
NETWORK_AUTHENTICATION_REQUIRED: HttpErrorConstant;
};