drizzle-team / drizzle-team/drizzle-orm-docs

`TransactionRollbackError` behavior isn't documented

Open
#269 5 comments 0 reactions 0 assignees View on GitHub
Dominant language
MDX
Stars
241
Forks
401
Avg merge
5h 44m
Merged PRs (30d)
1

Description

From [the transaction docs](https://orm.drizzle.team/docs/transactions), it appears that no error will be thrown by calling `rollback()` on a transaction, but doing so will throw a `TransactionRollbackError`. Should the docs be updated?

**Documented behavior**:
The code provided in the docs:
```ts
const db = drizzle(...)
await db.transaction(async (tx) => {
const [account] = await tx.select({ balance: accounts.balance }).from(accounts).where(eq(users.name, 'Dan'));
if (account.balance < 100) {
await tx.rollback()
return
}
await tx.update(accounts).set({ balance: sql`${accounts.balance} - 100.00` }).where(eq(users.name, 'Dan'));
await tx.update(accounts).set({ balance: sql`${accounts.balance} + 100.00` }).where(eq(users.name, 'Andrew'));
});
```
seems to be misleading, as the `await` before `tx.rollback()` is a no-op, and the `return` underneath will never be called. What will actually happen is that `rollback()` will throw an error, which is uncaught in this code example.

**Actual behavior**:
```ts
const db = drizzle(...)
await db.transaction(async (tx) => {
const [account] = await tx.select({ balance: accounts.balance }).from(accounts).where(eq(users.name, 'Dan'));
if (account.balance < 100) {
// Throws a `TransactionRollbackError`
tx.rollback();
}
await tx.update(accounts).set({ balance: sql`${accounts.balance} - 100.00` }).where(eq(users.name, 'Dan'));
await tx.update(accounts).set({ balance: sql`${accounts.balance} + 100.00` }).where(eq(users.name, 'Andrew'));
});
```

This suggests that throwing was unintentional behavior, but according to [this issue](https://github.com/drizzle-team/drizzle-orm/issues/1447#issuecomment-1786830459) in the `drizzle-orm` repo, it is intentional but undocumented.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.