Added async validation

This commit is contained in:
Dries Augustyns
2025-12-01 15:21:12 +01:00
parent 1f3978e89c
commit 361ff6fede
20 changed files with 240 additions and 109 deletions
+24
View File
@@ -0,0 +1,24 @@
import type {NextFunction, Request, Response} from 'express';
/**
* Method decorator to catch async errors in OvernightJS controllers
* Wraps async controller methods to ensure errors are passed to Express error handler
*
* Usage:
* @Post('route')
* @CatchAsync
* public async myMethod(req: Request, res: Response) { ... }
*
* This decorator ensures all errors (sync and async) thrown in the method
* are caught and passed to the Express error middleware, fixing the hanging
* request issue when Zod validation fails.
*/
export function CatchAsync(target: any, propertyKey: string, descriptor: PropertyDescriptor) {
const originalMethod = descriptor.value;
descriptor.value = function (req: Request, res: Response, next: NextFunction) {
Promise.resolve(originalMethod.call(this, req, res, next)).catch(next);
};
return descriptor;
}