Skip to content

fix(prisma): settle a recorded query without leaking its rejection - #238

Open
evlawler wants to merge 1 commit into
mainfrom
fix/prisma-settle-rejected-queries
Open

evlawler wants to merge 1 commit into
mainfrom
fix/prisma-settle-rejected-queries

Conversation

@evlawler

Copy link
Copy Markdown

A Prisma query that fails and is caught by the application still ends the process with an unhandled rejection while recording.

Cause. The hook marks a recorded query as returned with void result.finally(...). finally() returns a new promise that rejects whenever the query rejects. Nothing awaits it. Under vitest the run exits 1 and no recording is written. A caught P2002 unique violation in a sign up path is where it showed.

Fix. Attach the callback with result.then(settle, settle). functionReturn already attaches its own handlers to the query's promise and rewrites the return event as an exception event when the promise rejects, so the recorded shape of a failed query does not change. Only the stray rejection goes away.

Test. test/prisma/script.js now runs a create that fails on the unique email and catches it. Before the fix the fixture exits 1 at that statement. The snapshot gains the failed create as an exception event, with its SQL.

Written by Claude in a Claude Code session for Elizabeth Lawler. The commit carries Claude as author.

🤖 Generated with Claude Code

https://claude.ai/code/session_01EgK3BVLCovf26kotqTnkau


Generated by Claude Code

The Prisma hook records each model query as a function call and marks it
returned when the query's promise settles. It did that with
`void result.finally(...)`. finally() returns a new promise that rejects
whenever the query rejects, and nothing awaited it, so a query that failed
and was caught by the application still surfaced as an unhandled rejection.
Under vitest that ends the run with exit code 1 and no recording is written.
A caught P2002 unique violation in a sign up path is what showed it.

Attach the settle callback with then(settle, settle) instead. functionReturn
already attaches its own handlers to the query's promise and rewrites the
return event as an exception event when the promise rejects, so the recorded
shape of a failed query is unchanged. Only the stray rejection goes away.

The Prisma fixture now includes a create that fails on the unique email and
is caught. Before this change the fixture exits 1 on that statement. The
snapshot gains the failed create, recorded as an exception, and its SQL.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EgK3BVLCovf26kotqTnkau
@evlawler
evlawler marked this pull request as ready for review September 16, 2026 11:58
Copilot AI lite review requested due to automatic review settings September 16, 2026 11:58

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

No unresolved blocking issues were identified.

Pull request overview

This PR fixes Prisma query recording so caught rejected queries do not trigger unhandled rejections while preserving exception events.

Changes:

  • Replaces finally() with rejection-safe then(settle, settle).
  • Adds a caught P2002 failure test.
  • Updates the expected Prisma snapshot.
File summaries
File Description
test/prisma/script.js Exercises a caught unique-constraint failure.
test/__snapshots__/prisma.test.ts.snap Records expected exception and SQL events.
src/hooks/prisma.ts Safely settles Prisma promises.
Review details
  • Files reviewed: 3/3 changed files
  • Comments generated: 0
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

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