Transaction rollback error catching

I'm using drizzle with TRPC and want my api to give detailed reasons for a rollback. However tx.rollback() throws a TransactionRollbackError, which means the TRPC error is never thrown (i think). This means the client sees an error with message "Rollback", which isn't super descriptive.

await db.transaction(async (tx) => {
  ...some logic
  if (logicFails) {
    tx.rollback();
    throw new TRPCError({ code: "CONFLICT", message: "Detailed reason..." });
  }
});


Is it safe to catch the drizzle error inside of the transaction and throw a TRPC one instead? Im guessing so but just wanted to check.

await db.transaction(async (tx) => {
  ...some logic
  if (logicFails) {
    try {
      tx.rollback();
    } catch (e) { };
    throw new TRPCError({ code: "CONFLICT", message: "Detailed reason..." });
  }
});
Was this page helpful?