]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/middlewares/async.ts
Refractor retry transaction function
[github/Chocobozzz/PeerTube.git] / server / middlewares / async.ts
index dd209b115d2d46cb4701896dbdf5900e7c9d13a8..f770bc1209e27996e37f0c7fe51a6046df4f1fc1 100644 (file)
@@ -1,5 +1,6 @@
 import { eachSeries } from 'async'
 import { NextFunction, Request, RequestHandler, Response } from 'express'
+import { retryTransactionWrapper } from '../helpers/database-utils'
 
 // Syntactic sugar to avoid try/catch in express controllers
 // Thanks: https://medium.com/@Abazhenov/using-async-await-in-express-with-node-8-b8af872c0016
@@ -20,8 +21,17 @@ function asyncMiddleware (fun: RequestPromiseHandler | RequestPromiseHandler[])
   }
 }
 
+function asyncRetryTransactionMiddleware (fun: RequestPromiseHandler) {
+  return (req: Request, res: Response, next: NextFunction) => {
+    return Promise.resolve(
+      retryTransactionWrapper(fun, req, res, next)
+    ).catch(err => next(err))
+  }
+}
+
 // ---------------------------------------------------------------------------
 
 export {
-  asyncMiddleware
+  asyncMiddleware,
+  asyncRetryTransactionMiddleware
 }