UNPKG

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.74 kB
"use strict";var S=Object.defineProperty;var W=Object.getOwnPropertyDescriptor;var Q=Object.getOwnPropertyNames;var J=Object.prototype.hasOwnProperty;var Z=(r,t)=>{for(var o in t)S(r,o,{get:t[o],enumerable:!0})},z=(r,t,o,u)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of Q(t))!J.call(r,n)&&n!==o&&S(r,n,{get:()=>t[n],enumerable:!(u=W(t,n))||u.enumerable});return r};var X=r=>z(S({},"__esModule",{value:!0}),r);var rr={};Z(rr,{BadRequestError:()=>d,ConflictError:()=>E,CustomAPIError:()=>e,ForbiddenError:()=>g,NotFoundError:()=>c,StatusCodes:()=>$.StatusCodes,TooManyRequestsError:()=>b,UnauthenticatedError:()=>f,ValidationError:()=>x,asyncHandler:()=>L,getStatusMessage:()=>$.getStatusMessage,globalErrorHandler:()=>Y,httpError:()=>F,isCustomAPIError:()=>C,notFoundHandler:()=>H,setErrorOptions:()=>B});module.exports=X(rr);var p=require("http-status-toolkit");var _=require("http-status-toolkit"),e=class extends Error{constructor(t,o=_.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 D=require("http-status-toolkit");var d=class extends e{constructor(t,o=null){super(t,D.StatusCodes.BAD_REQUEST,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.UNAUTHORIZED,o),this.name=this.constructor.name}};var T=require("http-status-toolkit");var E=class extends e{constructor(t,o=null){super(t,T.StatusCodes.CONFLICT,o),this.name=this.constructor.name}};var w=require("http-status-toolkit");var g=class extends e{constructor(t,o=null){super(t,w.StatusCodes.FORBIDDEN,o),this.name=this.constructor.name}};var k=require("http-status-toolkit");var x=class extends e{constructor(t,o=null){super(t,k.StatusCodes.UNPROCESSABLE_ENTITY,o),this.name=this.constructor.name}};var j=require("http-status-toolkit");var b=class extends e{constructor(t,o=null){super(t,j.StatusCodes.TOO_MANY_REQUESTS,o),this.name=this.constructor.name}};function F(r,t,o=null){return new e(r,t,o)}var L=r=>(t,o,u)=>{Promise.resolve(r(t,o,u)).catch(u)};var H=(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 O(r){return`${s.bold}${s.red}${r}${s.reset}`}function A(r){return`${s.red}${r}${s.reset}`}function U(r){return`${s.bold}${s.yellow}${r}${s.reset}`}function q(r){return`${s.yellow}${r}${s.reset}`}function v(r){return`${s.bold}${s.green}${r}${s.reset}`}function M(r){return`${s.green}${r}${s.reset}`}function G(r){return`${s.bold}${r}${s.reset}`}function V(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 B(r){m={...m,...r}}var Y=(r,t,o,u)=>{let n=R.StatusCodes.INTERNAL_SERVER_ERROR,a=(0,R.getStatusMessage)(R.StatusCodes.INTERNAL_SERVER_ERROR),h,N;if(r instanceof Error){if(C(r)){let l=r;n=l.statusCode||n,a=l.message.trim()||a,h=l.errorDetails}else a=r.message||a;N=r.stack}let i={success:!1,status:n,message:a};if(h&&(i.errorDetails=h),N&&m.showStack&&(i.stack=N.split(` `).map(l=>l.trim())),m.logError&&(m.introLine?console.error(` ${G(`${m.introLine}`)} `):console.error(),console.error(`${O("\u{1F534} Error Status:")} ${A(String(i.status))} `),console.error(`${O("\u{1F534} Error Message:")} ${A(i.message)} `),i.errorDetails&&(console.error(U("\u{1F7E1} Error Details:")),console.error(q(typeof i.errorDetails=="object"?JSON.stringify(i.errorDetails,null,2):i.errorDetails)),console.error()),i.stack)){let l=i.stack,I=l.slice(0,3),P=l.length-I.length;console.error(v("\u{1F7E2} Stack Trace (Preview):")),I.forEach(K=>console.error(M(K))),P>0&&console.error(V(`...and ${P} more lines. \u{1F4A1} See full stack in error response`))}o.status(n).json(i)};var $=require("http-status-toolkit");0&&(module.exports={BadRequestError,ConflictError,CustomAPIError,ForbiddenError,NotFoundError,StatusCodes,TooManyRequestsError,UnauthenticatedError,ValidationError,asyncHandler,getStatusMessage,globalErrorHandler,httpError,isCustomAPIError,notFoundHandler,setErrorOptions});