Skip to content

Commit

Permalink
utils: Create module.
Browse files Browse the repository at this point in the history
Added the catchError function to be used with controllers.
  • Loading branch information
kuv2707 committed Jun 4, 2024
1 parent 0623dca commit 4c08d18
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions backend/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Request, Response } from 'express';

type ControllerFunction = (req: Request, res: Response) => Promise<void>;

export function catchError(fn: ControllerFunction): ControllerFunction {
return async function (req: Request, res: Response) {
try {
return await fn(req, res);
} catch (error) {
res.status(500).json({ error: error });
}
};
}

0 comments on commit 4c08d18

Please sign in to comment.