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) 3.88 kB
import{getStatusMessage as j,StatusCodes as O}from"http-status-toolkit";import{StatusCodes as k}from"http-status-toolkit";var e=class extends Error{constructor(o,t=k.INTERNAL_SERVER_ERROR,u=null){super(o),this.statusCode=t,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(o,t=null){super(o||j(O.NOT_FOUND),O.NOT_FOUND,t),this.name=this.constructor.name}};import{StatusCodes as F}from"http-status-toolkit";var p=class extends e{constructor(o,t=null){super(o,F.BAD_REQUEST,t),this.name=this.constructor.name}};import{StatusCodes as L}from"http-status-toolkit";var d=class extends e{constructor(o,t=null){super(o,L.UNAUTHORIZED,t),this.name=this.constructor.name}};import{StatusCodes as H}from"http-status-toolkit";var E=class extends e{constructor(o,t=null){super(o,H.CONFLICT,t),this.name=this.constructor.name}};import{StatusCodes as U}from"http-status-toolkit";var f=class extends e{constructor(o,t=null){super(o,U.FORBIDDEN,t),this.name=this.constructor.name}};import{StatusCodes as q}from"http-status-toolkit";var g=class extends e{constructor(o,t=null){super(o,q.UNPROCESSABLE_ENTITY,t),this.name=this.constructor.name}};import{StatusCodes as v}from"http-status-toolkit";var x=class extends e{constructor(o,t=null){super(o,v.TOO_MANY_REQUESTS,t),this.name=this.constructor.name}};function G(r,o,t=null){return new e(r,o,t)}var M=r=>(o,t,u)=>{Promise.resolve(r(o,t,u)).catch(u)};var V=(r,o,t)=>{let u=r.method||"UNKNOWN_METHOD",l=r.originalUrl||"UNKNOWN_URL";t(new c(`Cannot ${u} ${l}`))};function C(r){return r instanceof Error&&"statusCode"in r&&typeof r.statusCode=="number"}import{StatusCodes as T,getStatusMessage as B}from"http-status-toolkit";var s={reset:"\x1B[0m",bold:"\x1B[1m",red:"\x1B[31m",green:"\x1B[32m",yellow:"\x1B[33m",dimGray:"\x1B[2m"};function $(r){return`${s.bold}${s.red}${r}${s.reset}`}function h(r){return`${s.red}${r}${s.reset}`}function A(r){return`${s.bold}${s.yellow}${r}${s.reset}`}function I(r){return`${s.yellow}${r}${s.reset}`}function P(r){return`${s.bold}${s.green}${r}${s.reset}`}function _(r){return`${s.green}${r}${s.reset}`}function D(r){return`${s.bold}${r}${s.reset}`}function y(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 Y(r){m={...m,...r}}var K=(r,o,t,u)=>{let l=T.INTERNAL_SERVER_ERROR,a=B(T.INTERNAL_SERVER_ERROR),b,R;if(r instanceof Error){if(C(r)){let i=r;l=i.statusCode||l,a=i.message.trim()||a,b=i.errorDetails}else a=r.message||a;R=r.stack}let n={success:!1,status:l,message:a};if(b&&(n.errorDetails=b),R&&m.showStack&&(n.stack=R.split(` `).map(i=>i.trim())),m.logError&&(m.introLine?console.error(` ${D(`${m.introLine}`)} `):console.error(),console.error(`${$("\u{1F534} Error Status:")} ${h(String(n.status))} `),console.error(`${$("\u{1F534} Error Message:")} ${h(n.message)} `),n.errorDetails&&(console.error(A("\u{1F7E1} Error Details:")),console.error(I(typeof n.errorDetails=="object"?JSON.stringify(n.errorDetails,null,2):n.errorDetails)),console.error()),n.stack)){let i=n.stack,N=i.slice(0,3),S=i.length-N.length;console.error(P("\u{1F7E2} Stack Trace (Preview):")),N.forEach(w=>console.error(_(w))),S>0&&console.error(y(`...and ${S} more lines. \u{1F4A1} See full stack in error response`))}t.status(l).json(n)};export{p as BadRequestError,E as ConflictError,e as CustomAPIError,f as ForbiddenError,c as NotFoundError,x as TooManyRequestsError,d as UnauthenticatedError,g as ValidationError,M as asyncHandler,K as globalErrorHandler,G as httpError,C as isCustomAPIError,V as notFoundHandler,Y as setErrorOptions};