@otterhttp/errors
Version:
http-errors for modern Node.js
218 lines (215 loc) • 9.11 kB
JavaScript
// src/index.ts
import ModuleError from "module-error";
// src/status-codes.ts
var statusMessages = {
100: "Continue",
101: "Switching Protocols",
102: "Processing",
103: "Early Hints",
200: "OK",
201: "Created",
202: "Accepted",
203: "Non-Authoritative Information",
204: "No Content",
205: "Reset Content",
206: "Partial Content",
207: "Multi-Status",
208: "Already Reported",
226: "IM Used",
300: "Multiple Choices",
301: "Moved Permanently",
302: "Found",
303: "See Other",
304: "Not Modified",
305: "Use Proxy",
307: "Temporary Redirect",
308: "Permanent Redirect",
400: "Bad Request",
401: "Unauthorized",
402: "Payment Required",
403: "Forbidden",
404: "Not Found",
405: "Method Not Allowed",
406: "Not Acceptable",
407: "Proxy Authentication Required",
408: "Request Timeout",
409: "Conflict",
410: "Gone",
411: "Length Required",
412: "Precondition Failed",
413: "Payload Too Large",
414: "URI Too Long",
415: "Unsupported Media Type",
416: "Range Not Satisfiable",
417: "Expectation Failed",
418: "I'm a Teapot",
421: "Misdirected Request",
422: "Unprocessable Entity",
423: "Locked",
424: "Failed Dependency",
425: "Too Early",
426: "Upgrade Required",
428: "Precondition Required",
429: "Too Many Requests",
431: "Request Header Fields Too Large",
451: "Unavailable For Legal Reasons",
500: "Internal Server Error",
501: "Not Implemented",
502: "Bad Gateway",
503: "Service Unavailable",
504: "Gateway Timeout",
505: "HTTP Version Not Supported",
506: "Variant Also Negotiates",
507: "Insufficient Storage",
508: "Loop Detected",
509: "Bandwidth Limit Exceeded",
510: "Not Extended",
511: "Network Authentication Required"
};
var HttpStatus = /* @__PURE__ */ ((HttpStatus2) => {
HttpStatus2[HttpStatus2["Continue"] = 100] = "Continue";
HttpStatus2[HttpStatus2["SwitchingProtocols"] = 101] = "SwitchingProtocols";
HttpStatus2[HttpStatus2["Processing"] = 102] = "Processing";
HttpStatus2[HttpStatus2["EarlyHints"] = 103] = "EarlyHints";
HttpStatus2[HttpStatus2["OK"] = 200] = "OK";
HttpStatus2[HttpStatus2["Created"] = 201] = "Created";
HttpStatus2[HttpStatus2["Accepted"] = 202] = "Accepted";
HttpStatus2[HttpStatus2["NonAuthoritativeInformation"] = 203] = "NonAuthoritativeInformation";
HttpStatus2[HttpStatus2["NoContent"] = 204] = "NoContent";
HttpStatus2[HttpStatus2["ResetContent"] = 205] = "ResetContent";
HttpStatus2[HttpStatus2["PartialContent"] = 206] = "PartialContent";
HttpStatus2[HttpStatus2["MultiStatus"] = 207] = "MultiStatus";
HttpStatus2[HttpStatus2["AlreadyReported"] = 208] = "AlreadyReported";
HttpStatus2[HttpStatus2["IMUsed"] = 226] = "IMUsed";
HttpStatus2[HttpStatus2["MultipleChoices"] = 300] = "MultipleChoices";
HttpStatus2[HttpStatus2["MovedPermanently"] = 301] = "MovedPermanently";
HttpStatus2[HttpStatus2["Found"] = 302] = "Found";
HttpStatus2[HttpStatus2["SeeOther"] = 303] = "SeeOther";
HttpStatus2[HttpStatus2["NotModified"] = 304] = "NotModified";
HttpStatus2[HttpStatus2["UseProxy"] = 305] = "UseProxy";
HttpStatus2[HttpStatus2["TemporaryRedirect"] = 307] = "TemporaryRedirect";
HttpStatus2[HttpStatus2["PermanentRedirect"] = 308] = "PermanentRedirect";
HttpStatus2[HttpStatus2["BadRequest"] = 400] = "BadRequest";
HttpStatus2[HttpStatus2["Unauthorized"] = 401] = "Unauthorized";
HttpStatus2[HttpStatus2["PaymentRequired"] = 402] = "PaymentRequired";
HttpStatus2[HttpStatus2["Forbidden"] = 403] = "Forbidden";
HttpStatus2[HttpStatus2["NotFound"] = 404] = "NotFound";
HttpStatus2[HttpStatus2["MethodNotAllowed"] = 405] = "MethodNotAllowed";
HttpStatus2[HttpStatus2["NotAcceptable"] = 406] = "NotAcceptable";
HttpStatus2[HttpStatus2["ProxyAuthenticationRequired"] = 407] = "ProxyAuthenticationRequired";
HttpStatus2[HttpStatus2["RequestTimeout"] = 408] = "RequestTimeout";
HttpStatus2[HttpStatus2["Conflict"] = 409] = "Conflict";
HttpStatus2[HttpStatus2["Gone"] = 410] = "Gone";
HttpStatus2[HttpStatus2["LengthRequired"] = 411] = "LengthRequired";
HttpStatus2[HttpStatus2["PreconditionFailed"] = 412] = "PreconditionFailed";
HttpStatus2[HttpStatus2["PayloadTooLarge"] = 413] = "PayloadTooLarge";
HttpStatus2[HttpStatus2["URITooLong"] = 414] = "URITooLong";
HttpStatus2[HttpStatus2["UnsupportedMediaType"] = 415] = "UnsupportedMediaType";
HttpStatus2[HttpStatus2["RangeNotSatisfiable"] = 416] = "RangeNotSatisfiable";
HttpStatus2[HttpStatus2["ExpectationFailed"] = 417] = "ExpectationFailed";
HttpStatus2[HttpStatus2["IAmATeapot"] = 418] = "IAmATeapot";
HttpStatus2[HttpStatus2["MisdirectedRequest"] = 421] = "MisdirectedRequest";
HttpStatus2[HttpStatus2["UnprocessableEntity"] = 422] = "UnprocessableEntity";
HttpStatus2[HttpStatus2["Locked"] = 423] = "Locked";
HttpStatus2[HttpStatus2["FailedDependency"] = 424] = "FailedDependency";
HttpStatus2[HttpStatus2["TooEarly"] = 425] = "TooEarly";
HttpStatus2[HttpStatus2["UpgradeRequired"] = 426] = "UpgradeRequired";
HttpStatus2[HttpStatus2["PreconditionRequired"] = 428] = "PreconditionRequired";
HttpStatus2[HttpStatus2["TooManyRequests"] = 429] = "TooManyRequests";
HttpStatus2[HttpStatus2["RequestHeaderFieldsTooLarge"] = 431] = "RequestHeaderFieldsTooLarge";
HttpStatus2[HttpStatus2["UnavailableForLegalReasons"] = 451] = "UnavailableForLegalReasons";
HttpStatus2[HttpStatus2["InternalServerError"] = 500] = "InternalServerError";
HttpStatus2[HttpStatus2["NotImplemented"] = 501] = "NotImplemented";
HttpStatus2[HttpStatus2["BadGateway"] = 502] = "BadGateway";
HttpStatus2[HttpStatus2["ServiceUnavailable"] = 503] = "ServiceUnavailable";
HttpStatus2[HttpStatus2["GatewayTimeout"] = 504] = "GatewayTimeout";
HttpStatus2[HttpStatus2["HttpVersionNotSupported"] = 505] = "HttpVersionNotSupported";
HttpStatus2[HttpStatus2["VariantAlsoNegotiates"] = 506] = "VariantAlsoNegotiates";
HttpStatus2[HttpStatus2["InsufficientStorage"] = 507] = "InsufficientStorage";
HttpStatus2[HttpStatus2["LoopDetected"] = 508] = "LoopDetected";
HttpStatus2[HttpStatus2["BandwidthLimitExceeded"] = 509] = "BandwidthLimitExceeded";
HttpStatus2[HttpStatus2["NotExtended"] = 510] = "NotExtended";
HttpStatus2[HttpStatus2["NetworkAuthenticationRequired"] = 511] = "NetworkAuthenticationRequired";
return HttpStatus2;
})(HttpStatus || {});
function isValidStatusCode(statusCode) {
return typeof statusCode === "number" && Object.prototype.hasOwnProperty.call(statusMessages, statusCode);
}
// src/index.ts
var HttpError = class _HttpError extends ModuleError {
static {
_HttpError.prototype.name = "HttpError";
}
constructor(message, options) {
super(message || "", options);
if (typeof options === "object" && options != null) {
if (options.statusCode != null) {
if (!isValidStatusCode(options.statusCode)) {
throw new ModuleError(`an HTTPError was thrown with an invalid HTTP status code: ${options.statusCode}`, {
code: "ERR_INVALID_HTTP_ERROR_HTTP_STATUS_CODE"
});
}
this.statusCode = options.statusCode;
}
if (options.statusMessage != null) this.statusMessage = options.statusMessage;
if (options.exposeMessage != null) this.exposeMessage = options.exposeMessage;
if (options.headers != null) this.headers = options.headers;
if (options.extra != null) this.extra = options.extra;
}
this.statusCode ??= 500 /* InternalServerError */;
this.statusMessage ??= statusMessages[this.statusCode];
this.exposeMessage ??= this.statusCode < 500;
this.headers ??= {};
this.extra ??= {};
}
};
var NotModifiedError = class _NotModifiedError extends HttpError {
static {
_NotModifiedError.prototype.name = "NotModifiedError";
}
constructor(message, options) {
const superOptions = options ?? {};
superOptions.statusCode = 304 /* NotModified */;
superOptions.expected ??= true;
super(message, superOptions);
}
};
var ClientError = class _ClientError extends HttpError {
static {
_ClientError.prototype.name = "ClientError";
}
constructor(message, options = {}) {
options.expected ??= true;
options.statusCode ??= 400;
super(message, options);
if (this.statusCode < 400 || this.statusCode >= 500) {
throw new ModuleError(
`a ClientError was thrown with a disallowed HTTP status code: ${this.statusCode}. Only 4xx codes are allowed.`,
{ code: "ERR_INVALID_CLIENT_ERROR_HTTP_STATUS_CODE" }
);
}
}
};
var ServerError = class _ServerError extends HttpError {
static {
_ServerError.prototype.name = "ServerError";
}
constructor(message, options) {
super(message, options);
if (this.statusCode < 500) {
throw new ModuleError(
`a ServerError was thrown with an invalid HTTP status code: ${this.statusCode}. Only 5xx codes are allowed.`,
{ code: "ERR_INVALID_SERVER_ERROR_HTTP_STATUS_CODE" }
);
}
}
};
export {
ClientError,
HttpError,
HttpStatus,
NotModifiedError,
ServerError,
isValidStatusCode,
statusMessages
};