Skip to content

Fix transaction commit() to throw errors instead of swallowing them#558

Merged
KyleAMathews merged 7 commits intomainfrom
throw-on-commit
Sep 16, 2025
Merged

Fix transaction commit() to throw errors instead of swallowing them#558
KyleAMathews merged 7 commits intomainfrom
throw-on-commit

Conversation

@KyleAMathews
Copy link
Copy Markdown
Collaborator

Summary

  • Make transaction commit() method throw errors instead of swallowing them
  • Add test to verify the documented error handling behavior works correctly

Background

Previously, when a transaction's mutationFn threw an error, the commit() method would catch it, rollback the transaction, and return the failed transaction instead of re-throwing the error. This made it inconsistent with the documented error handling patterns shown in the docs.

Changes

  • Modified the commit() method to re-throw errors after rolling back
  • Added a test case that verifies both await tx.commit() and await tx.isPersisted.promise work correctly in try/catch blocks
  • Updated changeset with proper description

Test plan

  • Added test case that verifies commit() throws errors when mutation function fails
  • Existing tests still pass
  • Error handling now matches the documented examples

🤖 Generated with Claude Code

Previously, when a transaction's mutationFn threw an error, commit() would catch it, rollback the transaction, and return the failed transaction. This was inconsistent with documented error handling patterns.

Now commit() properly re-throws errors after rolling back, allowing both `await tx.commit()` and `await tx.isPersisted.promise` to work correctly in try/catch blocks.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Sep 15, 2025

🦋 Changeset detected

Latest commit: 01b8296

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 12 packages
Name Type
@tanstack/db Minor
@tanstack/angular-db Patch
@tanstack/electric-db-collection Patch
@tanstack/query-db-collection Patch
@tanstack/react-db Patch
@tanstack/rxdb-db-collection Patch
@tanstack/solid-db Patch
@tanstack/svelte-db Patch
@tanstack/trailbase-db-collection Patch
@tanstack/vue-db Patch
todos Patch
@tanstack/db-example-react-todo Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented Sep 15, 2025

More templates

@tanstack/angular-db

npm i https://pkg.pr.new/@tanstack/angular-db@558

@tanstack/db

npm i https://pkg.pr.new/@tanstack/db@558

@tanstack/db-ivm

npm i https://pkg.pr.new/@tanstack/db-ivm@558

@tanstack/electric-db-collection

npm i https://pkg.pr.new/@tanstack/electric-db-collection@558

@tanstack/query-db-collection

npm i https://pkg.pr.new/@tanstack/query-db-collection@558

@tanstack/react-db

npm i https://pkg.pr.new/@tanstack/react-db@558

@tanstack/rxdb-db-collection

npm i https://pkg.pr.new/@tanstack/rxdb-db-collection@558

@tanstack/solid-db

npm i https://pkg.pr.new/@tanstack/solid-db@558

@tanstack/svelte-db

npm i https://pkg.pr.new/@tanstack/svelte-db@558

@tanstack/trailbase-db-collection

npm i https://pkg.pr.new/@tanstack/trailbase-db-collection@558

@tanstack/vue-db

npm i https://pkg.pr.new/@tanstack/vue-db@558

commit: 01b8296

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Sep 15, 2025

Size Change: +18 B (+0.03%)

Total Size: 66.7 kB

Filename Size Change
./packages/db/dist/esm/transactions.js 2.3 kB +18 B (+0.79%)
ℹ️ View Unchanged
Filename Size
./packages/db/dist/esm/change-events.js 1.13 kB
./packages/db/dist/esm/collection.js 10.6 kB
./packages/db/dist/esm/deferred.js 230 B
./packages/db/dist/esm/errors.js 3.1 kB
./packages/db/dist/esm/index.js 1.55 kB
./packages/db/dist/esm/indexes/auto-index.js 745 B
./packages/db/dist/esm/indexes/base-index.js 605 B
./packages/db/dist/esm/indexes/btree-index.js 1.74 kB
./packages/db/dist/esm/indexes/lazy-index.js 1.25 kB
./packages/db/dist/esm/local-only.js 827 B
./packages/db/dist/esm/local-storage.js 2.02 kB
./packages/db/dist/esm/optimistic-action.js 294 B
./packages/db/dist/esm/proxy.js 3.87 kB
./packages/db/dist/esm/query/builder/functions.js 615 B
./packages/db/dist/esm/query/builder/index.js 3.93 kB
./packages/db/dist/esm/query/builder/ref-proxy.js 938 B
./packages/db/dist/esm/query/compiler/evaluators.js 1.52 kB
./packages/db/dist/esm/query/compiler/expressions.js 631 B
./packages/db/dist/esm/query/compiler/group-by.js 2.08 kB
./packages/db/dist/esm/query/compiler/index.js 2.27 kB
./packages/db/dist/esm/query/compiler/joins.js 2.52 kB
./packages/db/dist/esm/query/compiler/order-by.js 1.23 kB
./packages/db/dist/esm/query/compiler/select.js 1.28 kB
./packages/db/dist/esm/query/ir.js 508 B
./packages/db/dist/esm/query/live-query-collection.js 333 B
./packages/db/dist/esm/query/live/collection-config-builder.js 2.59 kB
./packages/db/dist/esm/query/live/collection-subscriber.js 2.4 kB
./packages/db/dist/esm/query/optimizer.js 3.05 kB
./packages/db/dist/esm/SortedMap.js 1.24 kB
./packages/db/dist/esm/utils.js 943 B
./packages/db/dist/esm/utils/btree.js 6.02 kB
./packages/db/dist/esm/utils/comparison.js 718 B
./packages/db/dist/esm/utils/index-optimization.js 1.62 kB

compressed-size-action::db-package-size

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Sep 15, 2025

Size Change: 0 B

Total Size: 1.18 kB

ℹ️ View Unchanged
Filename Size
./packages/react-db/dist/esm/index.js 152 B
./packages/react-db/dist/esm/useLiveQuery.js 1.02 kB

compressed-size-action::react-db-package-size

KyleAMathews and others added 5 commits September 16, 2025 08:34
…wing

- Fixed test that wasn't setting autoCommit: false for manual commit
- Updated other transaction tests to properly await commit() calls
- Added .catch() to autoCommit logic to prevent unhandled rejections
- All tests now pass without unhandled promise rejections

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
…ve tests

Based on feedback, made the following improvements:

1. Preserve original error identity when throwing from commit()
   - Capture the original error before rollback
   - Throw the exact same error instance to maintain stack traces

2. Add comprehensive error handling tests:
   - Verify commit() and isPersisted.promise reject with same error instance
   - Handle non-Error throwables (strings, numbers, objects)
   - Test idempotence - calling commit() on completed/failed transactions
   - Test cascading rollbacks with proper error propagation

3. Fix cascading rollback behavior
   - Secondary rollbacks now correctly reject isPersisted.promise
   - tx2 rejects with undefined when rolled back due to tx1's failure

This ensures error handling is consistent, predictable, and maintains
error identity for better debugging.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
Since commit() now throws errors instead of returning a failed transaction,
this is a breaking change that requires a minor version bump.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
@KyleAMathews
Copy link
Copy Markdown
Collaborator Author

@pubkey tests started failing for the rxdb-collection — could you look at that when you have a chance?

@pubkey
Copy link
Copy Markdown
Contributor

pubkey commented Sep 16, 2025

@KyleAMathews Your test stores data that does not match the schema of the test-RxDB-database:

...schemaPath: '#/additionalProperties', keyword: 'additionalProperties', params: { additionalProperty: 'foo' }, message: 'must NOT have additional properties' 

So there is an additional property called foo

Copy link
Copy Markdown
Collaborator

@samwillis samwillis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other than the failing test with rxdb this look good to me.

@pubkey
Copy link
Copy Markdown
Contributor

pubkey commented Sep 16, 2025

Just got in a bit deeper.
Vitest caught 1 unhandled error during the test run

I think the problem is not the rxdb-adapter but how this change is handling errors and how vitest expects all errors to be catched/handled.

The reason why the rxdb-adapter fails seems to be because it is really testing the correct persistence-write-behavior on wrong data.

      await expect(async () => {
        const tx = await collection.insert({
          id: `3`,
          name: `invalid`,
          foo: `bar`,
        })
        await tx.isPersisted.promise
      }).rejects.toThrow(/schema validation error/)

@KyleAMathews
Copy link
Copy Markdown
Collaborator Author

@pubkey ok, I skipped the test for now.

Copy link
Copy Markdown
Collaborator

@samwillis samwillis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

@KyleAMathews KyleAMathews merged commit b5c87f7 into main Sep 16, 2025
6 checks passed
@KyleAMathews KyleAMathews deleted the throw-on-commit branch September 16, 2025 20:45
@github-actions github-actions Bot mentioned this pull request Sep 16, 2025
Uziniii pushed a commit to Uziniii/db that referenced this pull request Sep 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants