import type { NextFunction, Request, RequestHandler, Response } from "express"; /** * Wraps an async route handler so a rejected promise (or a thrown error before * the handler's own try/catch even starts, e.g. in a shared helper it calls) * is forwarded to Express's error middleware instead of becoming an unhandled * rejection — which, on modern Node, crashes the whole process rather than * just failing the one request. */ export function asyncHandler( fn: (req: Request, res: Response, next: NextFunction) => Promise, ): RequestHandler { return (req, res, next) => { fn(req, res, next).catch(next); }; }