Fix : handle transactions aborts

This commit is contained in:
2024-07-07 10:45:23 +02:00
parent aca90efc5d
commit d8cf8560c0
+18 -6
View File
@@ -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,
),
);
});
}