Fix : handle transactions aborts
This commit is contained in:
+18
-6
@@ -52,9 +52,19 @@ export function WithTransaction(
|
|||||||
const originalFunc = descriptor.value;
|
const originalFunc = descriptor.value;
|
||||||
|
|
||||||
descriptor.value = function (...args: any[]) {
|
descriptor.value = function (...args: any[]) {
|
||||||
return new Promise(async (resolve) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
if (args.at(-1) instanceof mongoose.mongo.ClientSession) {
|
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);
|
resolve(data);
|
||||||
return data;
|
return data;
|
||||||
} else {
|
} else {
|
||||||
@@ -66,11 +76,13 @@ export function WithTransaction(
|
|||||||
})
|
})
|
||||||
.catch((e) => {
|
.catch((e) => {
|
||||||
if (e instanceof HttpException) {
|
if (e instanceof HttpException) {
|
||||||
throw e;
|
reject(e);
|
||||||
}
|
}
|
||||||
throw new HttpException(
|
reject(
|
||||||
'Another request is processing',
|
new HttpException(
|
||||||
HttpStatus.TOO_MANY_REQUESTS,
|
'Another request is processing',
|
||||||
|
HttpStatus.TOO_MANY_REQUESTS,
|
||||||
|
),
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user