Skip to content

Commit

Permalink
Add async error handling to FunctionalRouter
Browse files Browse the repository at this point in the history
  • Loading branch information
rakyi committed May 14, 2024
1 parent 5a56145 commit 9b4d68a
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions adminSiteServer/FunctionalRouter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import express, { Router } from "express"
import express, { NextFunction, Router } from "express"
import { Request, Response } from "./authentication.js"

// Little wrapper to automatically send returned objects as JSON, makes
Expand All @@ -13,13 +13,12 @@ export class FunctionalRouter {
}

wrap(callback: (req: Request, res: Response) => Promise<any>) {
return async (req: Request, res: Response) => {
return async (req: Request, res: Response, next: NextFunction) => {
try {
res.send(await callback(req, res))
} catch (e) {
// log errors to stderr and rethrow
console.error(e)
throw e
next(e)
}
}
}
Expand Down

0 comments on commit 9b4d68a

Please sign in to comment.