From d8cf8560c08a2699b3984bd98a6448efc5e78e0b Mon Sep 17 00:00:00 2001 From: legonzaur Date: Sun, 7 Jul 2024 10:45:23 +0200 Subject: [PATCH] Fix : handle transactions aborts --- src/games/utils.ts | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/games/utils.ts b/src/games/utils.ts index b04197b..fbb6a61 100644 --- a/src/games/utils.ts +++ b/src/games/utils.ts @@ -52,9 +52,19 @@ export function WithTransaction( const originalFunc = descriptor.value; descriptor.value = function (...args: any[]) { - return new Promise(async (resolve) => { + return new Promise(async (resolve, reject) => { if (args.at(-1) instanceof mongoose.mongo.ClientSession) { - const data = await originalFunc.apply(this, args); + const data = await originalFunc.apply(this, args).catch((e) => { + if (e instanceof HttpException) { + throw e; + } + reject( + new HttpException( + 'Another request is processing', + HttpStatus.TOO_MANY_REQUESTS, + ), + ); + }); resolve(data); return data; } else { @@ -66,11 +76,13 @@ export function WithTransaction( }) .catch((e) => { if (e instanceof HttpException) { - throw e; + reject(e); } - throw new HttpException( - 'Another request is processing', - HttpStatus.TOO_MANY_REQUESTS, + reject( + new HttpException( + 'Another request is processing', + HttpStatus.TOO_MANY_REQUESTS, + ), ); }); }