move catch outside of the TX

This commit is contained in:
x032205
2025-10-28 05:32:24 -04:00
parent 3ee9c3ac7d
commit d468c668f2
@@ -578,10 +578,10 @@ export const pamAccountServiceFactory = ({
for (let i = 0; i < accounts.length; i += ROTATION_CONCURRENCY_LIMIT) {
const batch = accounts.slice(i, i + ROTATION_CONCURRENCY_LIMIT);
const rotationPromises = batch.map(async (account) =>
pamAccountDAL.transaction(async (tx) => {
const rotationPromises = batch.map(async (account) => {
let logResourceType = "unknown";
try {
await pamAccountDAL.transaction(async (tx) => {
const resource = await pamResourceDAL.findById(account.resourceId, tx);
if (!resource || !resource.encryptedRotationAccountCredentials) return;
logResourceType = resource.resourceType;
@@ -645,6 +645,7 @@ export const pamAccountServiceFactory = ({
}
}
});
});
} catch (error) {
logger.error(error, `Failed to rotate credentials for account [accountId=${account.id}]`);
@@ -659,14 +660,10 @@ export const pamAccountServiceFactory = ({
plainText: Buffer.from(errorMessage)
});
await pamAccountDAL.updateById(
account.id,
{
await pamAccountDAL.updateById(account.id, {
rotationStatus: "failed",
encryptedLastRotationMessage: encryptedMessage
},
tx
);
});
await auditLogService.createAuditLog({
projectId: account.projectId,
@@ -685,11 +682,8 @@ export const pamAccountServiceFactory = ({
}
}
});
throw error;
}
})
);
});
// eslint-disable-next-line no-await-in-loop
await Promise.all(rotationPromises);