Fix : handle transactions aborts
This commit is contained in:
+16
-4
@@ -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(
|
||||
reject(
|
||||
new HttpException(
|
||||
'Another request is processing',
|
||||
HttpStatus.TOO_MANY_REQUESTS,
|
||||
),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user