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..." });
}
});
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..." });
}
});
await db.transaction(async (tx) => {
...some logic
if (logicFails) {
try {
tx.rollback();
} catch (e) { };
throw new TRPCError({ code: "CONFLICT", message: "Detailed reason..." });
}
});
3 Replies
ReV
ReV10mo ago
need help too
Angelelz
Angelelz10mo ago
Yes, you can do this. However, you don't need to call tx.rollback(). Throwing the TRPC error inside the transaction will trigger the rollback
ReV
ReV10mo ago
i see thank youu
Want results from more Discord servers?
Add your server