zumito-framework 1.7.4 → 1.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -213,21 +213,6 @@ export class ZumitoFramework {
213
213
  this.app.use(express.json());
214
214
  this.app.use(express.urlencoded({ extended: false }));
215
215
  this.app.use(cookieParser());
216
- //Error handler
217
- this.app.use(function (err, req, res, next) {
218
- const errorHandler = ServiceContainer.getService(ErrorHandler);
219
- errorHandler.handleError(err, {
220
- type: ErrorType.Api,
221
- endpoint: req.originalUrl,
222
- method: req.method,
223
- });
224
- if (!res.headersSent) {
225
- return res.status(500).json({
226
- error: 'Internal Server Error',
227
- message: 'An unexpected error occurred. Please try again later.',
228
- });
229
- }
230
- });
231
216
  //this.app.use(express.static(path.join(__dirname, "public")));
232
217
  //To allow cross-origin requests
233
218
  this.app.use(cors());
@@ -235,18 +220,39 @@ export class ZumitoFramework {
235
220
  //this.app.use("/", indexRouter);
236
221
  //this.app.use("/api/", apiRouter);
237
222
  this.routes.forEach(route => {
238
- this.app[route.method](route.path, function (req, res) {
239
- return route.execute(req, res);
223
+ this.app[route.method](route.path, async (req, res, next) => {
224
+ try {
225
+ await Promise.resolve(route.execute(req, res));
226
+ }
227
+ catch (err) {
228
+ next(err);
229
+ }
240
230
  });
241
231
  });
242
232
  // throw 404 if URL not found
243
- this.app.all('*', function (req, res) {
233
+ this.app.all('*', (req, res) => {
244
234
  return ApiResponse.notFoundResponse(res, 'Page not found');
245
235
  });
246
- this.app.use(function (err, req, res) {
236
+ this.app.use((err, req, res, next) => {
247
237
  if (err.name === 'UnauthorizedError') {
248
238
  return ApiResponse.unauthorizedResponse(res, 'Invalid token');
249
239
  }
240
+ next(err);
241
+ });
242
+ //Error handler
243
+ this.app.use((err, req, res, next) => {
244
+ const errorHandler = ServiceContainer.getService(ErrorHandler);
245
+ errorHandler.handleError(err, {
246
+ type: ErrorType.Api,
247
+ endpoint: req.originalUrl,
248
+ method: req.method,
249
+ });
250
+ if (!res.headersSent) {
251
+ return res.status(500).json({
252
+ error: 'Internal Server Error',
253
+ message: 'An unexpected error occurred. Please try again later.',
254
+ });
255
+ }
250
256
  });
251
257
  }
252
258
  /**
@@ -33,6 +33,8 @@ export class ErrorHandler {
33
33
  console.error(error?.toString?.() || 'Unknown error');
34
34
  console.line('');
35
35
  }
36
+ // Emit framework error event so external modules can listen and report
37
+ this.framework.eventEmitter.emit('error', error, options);
36
38
  this.printErrorStack(error);
37
39
  if (options.exit)
38
40
  process.exit(1);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zumito-framework",
3
- "version": "1.7.4",
3
+ "version": "1.8.1",
4
4
  "description": "Discord.js bot framework",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",