diff --git a/packages/plugin-search/src/utilities/generateReindexHandler.ts b/packages/plugin-search/src/utilities/generateReindexHandler.ts index c10ad0caa..91b97f38e 100644 --- a/packages/plugin-search/src/utilities/generateReindexHandler.ts +++ b/packages/plugin-search/src/utilities/generateReindexHandler.ts @@ -147,7 +147,7 @@ export const generateReindexHandler = } } - await initTransaction(req) + const shouldCommit = await initTransaction(req) try { const promises = collections.map(async (collection) => { @@ -157,14 +157,14 @@ export const generateReindexHandler = } catch (err) { const message = t('error:unableToReindexCollection', { collection }) payload.logger.error({ err, msg: message }) - - await killTransaction(req) - throw new Error(message) } }) await Promise.all(promises) } catch (err: any) { + if (shouldCommit) { + await killTransaction(req) + } return Response.json({ message: err.message }, { headers, status: 500 }) } @@ -174,7 +174,9 @@ export const generateReindexHandler = total: aggregateDocs, }) - await commitTransaction(req) + if (shouldCommit) { + await commitTransaction(req) + } return Response.json({ message }, { headers, status: 200 }) }