http-error-middleware
Version:
A simple Express middleware to handle and format error messages, improving error management in your applications.
151 lines (147 loc) • 6.25 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
for (var prop in b || (b = {}))
if (__hasOwnProp.call(b, prop))
__defNormalProp(a, prop, b[prop]);
if (__getOwnPropSymbols)
for (var prop of __getOwnPropSymbols(b)) {
if (__propIsEnum.call(b, prop))
__defNormalProp(a, prop, b[prop]);
}
return a;
};
var __objRest = (source, exclude) => {
var target = {};
for (var prop in source)
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
target[prop] = source[prop];
if (source != null && __getOwnPropSymbols)
for (var prop of __getOwnPropSymbols(source)) {
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
target[prop] = source[prop];
}
return target;
};
// src/enums/StatusCode.ts
var SuccessfulCodes = /* @__PURE__ */ ((SuccessfulCodes2) => {
SuccessfulCodes2[SuccessfulCodes2["ok"] = 200] = "ok";
SuccessfulCodes2[SuccessfulCodes2["created"] = 201] = "created";
SuccessfulCodes2[SuccessfulCodes2["accepted"] = 202] = "accepted";
SuccessfulCodes2[SuccessfulCodes2["NonAuthoritativeInformation"] = 203] = "NonAuthoritativeInformation";
SuccessfulCodes2[SuccessfulCodes2["noContent"] = 204] = "noContent";
return SuccessfulCodes2;
})(SuccessfulCodes || {});
var ClientErrorCodes = /* @__PURE__ */ ((ClientErrorCodes2) => {
ClientErrorCodes2[ClientErrorCodes2["badRequest"] = 400] = "badRequest";
ClientErrorCodes2[ClientErrorCodes2["unauthorized"] = 401] = "unauthorized";
ClientErrorCodes2[ClientErrorCodes2["paymentRequired"] = 402] = "paymentRequired";
ClientErrorCodes2[ClientErrorCodes2["forbidden"] = 403] = "forbidden";
ClientErrorCodes2[ClientErrorCodes2["notFound"] = 404] = "notFound";
ClientErrorCodes2[ClientErrorCodes2["methodNotAllowed"] = 405] = "methodNotAllowed";
ClientErrorCodes2[ClientErrorCodes2["notAcceptable"] = 406] = "notAcceptable";
ClientErrorCodes2[ClientErrorCodes2["proxyAuthenticationRequired"] = 407] = "proxyAuthenticationRequired";
ClientErrorCodes2[ClientErrorCodes2["requestTimeOut"] = 408] = "requestTimeOut";
ClientErrorCodes2[ClientErrorCodes2["conflict"] = 409] = "conflict";
return ClientErrorCodes2;
})(ClientErrorCodes || {});
var ServerErrorCodes = /* @__PURE__ */ ((ServerErrorCodes2) => {
ServerErrorCodes2[ServerErrorCodes2["internalServerError"] = 500] = "internalServerError";
ServerErrorCodes2[ServerErrorCodes2["notImplemented"] = 501] = "notImplemented";
ServerErrorCodes2[ServerErrorCodes2["badGateway"] = 502] = "badGateway";
ServerErrorCodes2[ServerErrorCodes2["serviceUnavailable"] = 503] = "serviceUnavailable";
ServerErrorCodes2[ServerErrorCodes2["gatewayTimeout"] = 504] = "gatewayTimeout";
return ServerErrorCodes2;
})(ServerErrorCodes || {});
// src/HttpError.ts
var HttpError = class _HttpError extends Error {
constructor(message, statusCode, details) {
super(message);
this.statusCode = statusCode;
this.details = details;
}
// Client errors
static badRequest(message, details) {
throw new _HttpError(message, 400 /* badRequest */, details);
}
static unauthorized(message, details) {
throw new _HttpError(message, 401 /* unauthorized */, details);
}
static paymentRequired(message, details) {
throw new _HttpError(message, 402 /* paymentRequired */, details);
}
static forbidden(message, details) {
throw new _HttpError(message, 403 /* forbidden */, details);
}
static notFound(message, details) {
throw new _HttpError(message, 404 /* notFound */, details);
}
static methodNotAllowed(message, details) {
throw new _HttpError(message, 405 /* methodNotAllowed */, details);
}
static notAcceptable(message, details) {
throw new _HttpError(message, 406 /* notAcceptable */, details);
}
static proxyAuthenticationRequired(message, details) {
throw new _HttpError(message, 407 /* proxyAuthenticationRequired */, details);
}
static requestTimeOut(message, details) {
throw new _HttpError(message, 408 /* requestTimeOut */, details);
}
static conflict(message, details) {
throw new _HttpError(message, 409 /* conflict */, details);
}
// Server errors
static internalServerError(message, details) {
throw new _HttpError(message, 500 /* internalServerError */, details);
}
static notImplemented(message, details) {
throw new _HttpError(message, 501 /* notImplemented */, details);
}
static badGateway(message, details) {
throw new _HttpError(message, 502 /* badGateway */, details);
}
static serviceUnavailable(message, details) {
throw new _HttpError(message, 503 /* serviceUnavailable */, details);
}
static gatewayTimeout(message, details) {
throw new _HttpError(message, 504 /* gatewayTimeout */, details);
}
/**
* Throw an error with status code not implemented yet in the package
*/
static custom(message, statusCode, details) {
throw new _HttpError(message, statusCode, details);
}
};
// src/middleware.ts
function httpErrorMiddleware(options = {}) {
return function(err, _req, res, next) {
if (err instanceof HttpError) {
const { destructure, statusCodeOnResponse } = options;
const { message, statusCode, details } = err;
let allData = { message, statusCode, details };
if (statusCodeOnResponse !== void 0 && !statusCodeOnResponse) {
const _a = allData, { statusCode: _ } = _a, restData = __objRest(_a, ["statusCode"]);
allData = restData;
}
if (destructure) {
const _b = allData, { details: destructureDetails } = _b, restData = __objRest(_b, ["details"]);
allData = __spreadValues(__spreadValues({}, restData), destructureDetails);
}
res.status(statusCode).json(__spreadValues({}, allData));
} else {
next(err);
}
};
}
export {
ClientErrorCodes,
HttpError,
ServerErrorCodes,
SuccessfulCodes,
httpErrorMiddleware
};