express-error-toolkit
Version:
A lightweight and developer-friendly toolkit for robust error handling in Express.js applications. Includes ready-to-use custom error classes, an async route error handler wrapper, a global error handler middleware, and a convenient 'not found' route hand
7 lines (6 loc) • 4.6 kB
JavaScript
var N=Object.defineProperty;var K=Object.getOwnPropertyDescriptor;var W=Object.getOwnPropertyNames;var Q=Object.prototype.hasOwnProperty;var J=(r,t)=>{for(var o in t)N(r,o,{get:t[o],enumerable:!0})},Z=(r,t,o,u)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of W(t))!Q.call(r,n)&&n!==o&&N(r,n,{get:()=>t[n],enumerable:!(u=K(t,n))||u.enumerable});return r};var z=r=>Z(N({},"__esModule",{value:!0}),r);var X={};J(X,{BadRequestError:()=>d,ConflictError:()=>f,CustomAPIError:()=>e,ForbiddenError:()=>g,NotFoundError:()=>c,TooManyRequestsError:()=>b,UnauthenticatedError:()=>E,ValidationError:()=>x,asyncHandler:()=>F,globalErrorHandler:()=>B,httpError:()=>j,isCustomAPIError:()=>C,notFoundHandler:()=>L,setErrorOptions:()=>V});module.exports=z(X);var p=require("http-status-toolkit");var P=require("http-status-toolkit"),e=class extends Error{constructor(t,o=P.StatusCodes.INTERNAL_SERVER_ERROR,u=null){super(t),this.statusCode=o,this.errorDetails=u,this.name=this.constructor.name,Object.setPrototypeOf(this,new.target.prototype),Error.captureStackTrace&&Error.captureStackTrace(this,this.constructor)}};var c=class extends e{constructor(t,o=null){super(t||(0,p.getStatusMessage)(p.StatusCodes.NOT_FOUND),p.StatusCodes.NOT_FOUND,o),this.name=this.constructor.name}};var _=require("http-status-toolkit");var d=class extends e{constructor(t,o=null){super(t,_.StatusCodes.BAD_REQUEST,o),this.name=this.constructor.name}};var D=require("http-status-toolkit");var E=class extends e{constructor(t,o=null){super(t,D.StatusCodes.UNAUTHORIZED,o),this.name=this.constructor.name}};var y=require("http-status-toolkit");var f=class extends e{constructor(t,o=null){super(t,y.StatusCodes.CONFLICT,o),this.name=this.constructor.name}};var T=require("http-status-toolkit");var g=class extends e{constructor(t,o=null){super(t,T.StatusCodes.FORBIDDEN,o),this.name=this.constructor.name}};var w=require("http-status-toolkit");var x=class extends e{constructor(t,o=null){super(t,w.StatusCodes.UNPROCESSABLE_ENTITY,o),this.name=this.constructor.name}};var k=require("http-status-toolkit");var b=class extends e{constructor(t,o=null){super(t,k.StatusCodes.TOO_MANY_REQUESTS,o),this.name=this.constructor.name}};function j(r,t,o=null){return new e(r,t,o)}var F=r=>(t,o,u)=>{Promise.resolve(r(t,o,u)).catch(u)};var L=(r,t,o)=>{let u=r.method||"UNKNOWN_METHOD",n=r.originalUrl||"UNKNOWN_URL";o(new c(`Cannot ${u} ${n}`))};function C(r){return r instanceof Error&&"statusCode"in r&&typeof r.statusCode=="number"}var R=require("http-status-toolkit");var s={reset:"\x1B[0m",bold:"\x1B[1m",red:"\x1B[31m",green:"\x1B[32m",yellow:"\x1B[33m",dimGray:"\x1B[2m"};function S(r){return`${s.bold}${s.red}${r}${s.reset}`}function O(r){return`${s.red}${r}${s.reset}`}function H(r){return`${s.bold}${s.yellow}${r}${s.reset}`}function U(r){return`${s.yellow}${r}${s.reset}`}function q(r){return`${s.bold}${s.green}${r}${s.reset}`}function v(r){return`${s.green}${r}${s.reset}`}function G(r){return`${s.bold}${r}${s.reset}`}function M(r){return`${s.dimGray}${r}${s.reset}`}var m={showStack:process.env.SHOW_STACK!=="false"&&process.env.NODE_ENV!=="production",logError:process.env.LOG_ERROR!=="false"&&process.env.NODE_ENV!=="production",introLine:"\u{1F4A5} Even the best code breaks sometimes! Let's debug..."};function V(r){m={...m,...r}}var B=(r,t,o,u)=>{let n=R.StatusCodes.INTERNAL_SERVER_ERROR,a=(0,R.getStatusMessage)(R.StatusCodes.INTERNAL_SERVER_ERROR),$,h;if(r instanceof Error){if(C(r)){let l=r;n=l.statusCode||n,a=l.message.trim()||a,$=l.errorDetails}else a=r.message||a;h=r.stack}let i={success:!1,status:n,message:a};if($&&(i.errorDetails=$),h&&m.showStack&&(i.stack=h.split(`
`).map(l=>l.trim())),m.logError&&(m.introLine?console.error(`
${G(`${m.introLine}`)}
`):console.error(),console.error(`${S("\u{1F534} Error Status:")} ${O(String(i.status))}
`),console.error(`${S("\u{1F534} Error Message:")} ${O(i.message)}
`),i.errorDetails&&(console.error(H("\u{1F7E1} Error Details:")),console.error(U(typeof i.errorDetails=="object"?JSON.stringify(i.errorDetails,null,2):i.errorDetails)),console.error()),i.stack)){let l=i.stack,A=l.slice(0,3),I=l.length-A.length;console.error(q("\u{1F7E2} Stack Trace (Preview):")),A.forEach(Y=>console.error(v(Y))),I>0&&console.error(M(`...and ${I} more lines. \u{1F4A1} See full stack in error response`))}o.status(n).json(i)};0&&(module.exports={BadRequestError,ConflictError,CustomAPIError,ForbiddenError,NotFoundError,TooManyRequestsError,UnauthenticatedError,ValidationError,asyncHandler,globalErrorHandler,httpError,isCustomAPIError,notFoundHandler,setErrorOptions});
;