error-express
Version:
Are you tired of messy error handling in your Express apps? Say goodbye to confusion and frustration with **`error-express`**, a powerful NPM package designed to streamline error management in your Express applications.
1 lines • 4.56 kB
Source Map (JSON)
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import Layer from \"express/lib/router/layer\";\nimport Router from \"express/lib/router\";\nimport { NextFunction, Request, Response } from \"express\";\n\nconst last = <T>(arr: T[] = []): T | undefined => arr[arr.length - 1];\nconst noop = Function.prototype as (...args: any[]) => void;\n\nfunction copyFnProps<T extends Function>(oldFn: T, newFn: T): T {\n Object.keys(oldFn).forEach((key) => {\n (newFn as any)[key] = (oldFn as any)[key];\n });\n return newFn;\n}\n\nfunction wrap<T extends Function>(fn: T): T {\n const newFn = function newFn(this: any, ...args: any[]) {\n const ret = fn.apply(this, args);\n const next = (args.length === 5 ? args[2] : last(args)) || noop;\n if (ret && ret.catch) ret.catch((err: any) => next(err));\n return ret;\n };\n Object.defineProperty(newFn, \"length\", {\n value: fn.length,\n writable: false,\n });\n return copyFnProps(fn, newFn as unknown as T);\n}\n\nfunction patchRouterParam() {\n const originalParam = Router.prototype.constructor.param;\n Router.prototype.constructor.param = function param(\n name: string,\n fn: Function\n ) {\n fn = wrap(fn);\n return originalParam.call(this, name, fn);\n };\n}\n\nObject.defineProperty(Layer.prototype, \"handle\", {\n enumerable: true,\n get() {\n return (this as any).__handle;\n },\n set(fn: Function) {\n fn = wrap(fn);\n (this as any).__handle = fn;\n },\n});\n\npatchRouterParam();\n\nexport interface IErrorResponse {\n message: string;\n statusCode: number;\n status: string;\n serializeErrors(): IError;\n}\n\nexport interface IError {\n message: string;\n statusCode: number;\n status: string;\n}\n\nexport abstract class CustomError extends Error {\n abstract status: string;\n private readonly statusCode: number;\n\n protected constructor(message: string, statusCode: number) {\n super(message);\n this.statusCode = statusCode;\n }\n\n serializeErrors(): IError {\n return {\n message: this.message,\n status: this.status,\n statusCode: this.statusCode,\n };\n }\n}\n\nexport class ServerError extends CustomError {\n status = \"error\";\n constructor(message: string, statusCode?: number) {\n super(message, statusCode || 500);\n }\n}\n\nexport const globalErrorHandler = (\n err: any,\n _req: Request,\n res: Response,\n _next: NextFunction\n) => {\n try {\n if (err instanceof CustomError) {\n res.status(err.serializeErrors().statusCode).json(err.serializeErrors());\n } else {\n\n res.status(500).json({\n message: err?.message || \"Server Error\",\n statusCode: 500,\n });\n }\n } catch (error) {\n res.status(500).json({\n message: \"Something went wrong\",\n statusCode: 500,\n });\n }\n};\n\n"],"mappings":"AAAA,OAAOA,MAAW,2BAClB,OAAOC,MAAY,qBAGnB,IAAMC,EAAO,CAAIC,EAAW,CAAC,IAAqBA,EAAIA,EAAI,OAAS,CAAC,EAC9DC,EAAO,SAAS,UAEtB,SAASC,EAAgCC,EAAUC,EAAa,CAC9D,cAAO,KAAKD,CAAK,EAAE,QAASE,GAAQ,CACjCD,EAAcC,CAAG,EAAKF,EAAcE,CAAG,CAC1C,CAAC,EACMD,CACT,CAEA,SAASE,EAAyBC,EAAU,CAC1C,IAAMH,EAAQ,YAA6BI,EAAa,CACtD,IAAMC,EAAMF,EAAG,MAAM,KAAMC,CAAI,EACzBE,GAAQF,EAAK,SAAW,EAAIA,EAAK,CAAC,EAAIT,EAAKS,CAAI,IAAMP,EAC3D,OAAIQ,GAAOA,EAAI,OAAOA,EAAI,MAAOE,GAAaD,EAAKC,CAAG,CAAC,EAChDF,CACT,EACA,cAAO,eAAeL,EAAO,SAAU,CACrC,MAAOG,EAAG,OACV,SAAU,EACZ,CAAC,EACML,EAAYK,EAAIH,CAAqB,CAC9C,CAEA,SAASQ,GAAmB,CAC1B,IAAMC,EAAgBf,EAAO,UAAU,YAAY,MACnDA,EAAO,UAAU,YAAY,MAAQ,SACnCgB,EACAP,EACA,CACA,OAAAA,EAAKD,EAAKC,CAAE,EACLM,EAAc,KAAK,KAAMC,EAAMP,CAAE,CAC1C,CACF,CAEA,OAAO,eAAeV,EAAM,UAAW,SAAU,CAC/C,WAAY,GACZ,KAAM,CACJ,OAAQ,KAAa,QACvB,EACA,IAAIU,EAAc,CAChBA,EAAKD,EAAKC,CAAE,EACX,KAAa,SAAWA,CAC3B,CACF,CAAC,EAEDK,EAAiB,EAeV,IAAeG,EAAf,cAAmC,KAAM,CAE7B,WAEP,YAAYC,EAAiBC,EAAoB,CACzD,MAAMD,CAAO,EACb,KAAK,WAAaC,CACpB,CAEA,iBAA0B,CACxB,MAAO,CACL,QAAS,KAAK,QACd,OAAQ,KAAK,OACb,WAAY,KAAK,UACnB,CACF,CACF,EAEaC,EAAN,cAA0BH,CAAY,CAC3C,OAAS,QACT,YAAYC,EAAiBC,EAAqB,CAChD,MAAMD,EAASC,GAAc,GAAG,CAClC,CACF,EAEaE,EAAqB,CAChCR,EACAS,EACAC,EACAC,IACG,CACH,GAAI,CACEX,aAAeI,EACjBM,EAAI,OAAOV,EAAI,gBAAgB,EAAE,UAAU,EAAE,KAAKA,EAAI,gBAAgB,CAAC,EAGvEU,EAAI,OAAO,GAAG,EAAE,KAAK,CACnB,QAASV,GAAK,SAAW,eACzB,WAAY,GACd,CAAC,CAEL,MAAgB,CACdU,EAAI,OAAO,GAAG,EAAE,KAAK,CACnB,QAAS,uBACT,WAAY,GACd,CAAC,CACH,CACF","names":["Layer","Router","last","arr","noop","copyFnProps","oldFn","newFn","key","wrap","fn","args","ret","next","err","patchRouterParam","originalParam","name","CustomError","message","statusCode","ServerError","globalErrorHandler","_req","res","_next"]}