Skip to content

fix(adapter-{pg,neon,ppg}): handle 22P02 error in Postgres#28849

Merged
aqrln merged 2 commits intomainfrom
aqrln-lywxxnsxxysk
Dec 5, 2025
Merged

fix(adapter-{pg,neon,ppg}): handle 22P02 error in Postgres#28849
aqrln merged 2 commits intomainfrom
aqrln-lywxxnsxxysk

Conversation

@aqrln
Copy link
Copy Markdown
Member

@aqrln aqrln commented Dec 4, 2025

Handle "invalid text representation"/"invalid input value" (22P02) errors when using Postgres with pg, neon, or ppg adapters, so that they are mapped to a proper user facing error.

See this 500 error in QPE that occurred in production: https://prisma-company.slack.com/archives/C04EPL7N2KV/p1764862201609579?thread_ts=1764849204.552389&cid=C04EPL7N2KV

Closes: https://linear.app/prisma-company/issue/TML-1664/fix-incorrect-500-error-returned-from-qpe-instead-of-400

Summary by CodeRabbit

  • New Features

    • Improved PostgreSQL error handling: invalid input values are now mapped to a clear InvalidInputValue variant and surfaced as Prisma error P2007.
    • Driver-adapter utilities now expose a new MappedError variant for invalid input values.
  • Documentation

    • Added a driver-adapter error handling guide explaining mapping and error-code derivation.
  • Tests

    • Added a regression test covering PostgreSQL enum type mismatch / invalid-enum-value scenarios.

✏️ Tip: You can customize this high-level summary in your review settings.

@aqrln aqrln added this to the 7.2.0 milestone Dec 4, 2025
@aqrln aqrln force-pushed the aqrln-lywxxnsxxysk branch from 57ed9b4 to 00e9c58 Compare December 4, 2025 16:40
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Dec 4, 2025

Walkthrough

Adds mapping for PostgreSQL error code 22P02 to a new MappedError variant InvalidInputValue, exports that variant from driver-adapter-utils, maps it to Prisma error code P2007 in the client runtime, and adds a regression test covering the enum-mismatch scenario.

Changes

Cohort / File(s) Summary
Public types / docs
AGENTS.md, packages/driver-adapter-utils/src/types.ts
Introduced and exported new MappedError variant { kind: 'InvalidInputValue', message: string } and documented driver-adapter error mapping and conversion rules.
Adapter error mapping
packages/adapter-neon/src/errors.ts, packages/adapter-pg/src/errors.ts, packages/adapter-ppg/src/errors.ts
Added handling for PostgreSQL error code 22P02, mapping it to { kind: 'InvalidInputValue', message: error.message } in mapDriverError.
Adapter tests
packages/adapter-pg/src/__tests__/errors.test.ts
Added test asserting conversion of 22P02 to InvalidInputValue, preserving message and original error info.
Client runtime mapping
packages/client-engine-runtime/src/user-facing-error.ts
Mapped InvalidInputValue to Prisma error code P2007 and render a user-facing message referencing the invalid input value.
Functional regression test
packages/client/tests/functional/issues/TML-1664-invalid-enum-value-error/_matrix.ts, .../prisma/_schema.ts, .../test.ts
Added test matrix, Prisma schema with Status enum, and a regression test that triggers DB enum mismatch by inserting a value missing from the DB enum and expects a P2007 PrismaClientKnownRequestError.
sequenceDiagram
  autonumber
  participant App as Application (test)
  participant Client as Prisma Client
  participant Adapter as DB Adapter (pg/neon/ppg)
  participant Utils as driver-adapter-utils
  participant DB as PostgreSQL

  App->>Client: perform write using enum value "PENDING"
  Client->>Adapter: send SQL insert
  Adapter->>DB: execute query
  DB-->>Adapter: error 22P02 (invalid input value for enum)
  Adapter->>Utils: mapDriverError(error 22P02)
  Utils-->>Adapter: { kind: "InvalidInputValue", message }
  Adapter-->>Client: throw converted driver error containing MappedError
  Client->>Client: map InvalidInputValue -> P2007 (user-facing error)
  Client-->>App: throw PrismaClientKnownRequestError (code P2007, message)
Loading

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly describes the main change: adding PostgreSQL error code 22P02 handling across three adapters. It is specific, concise, and accurately reflects the core purpose of the changeset.
✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch aqrln-lywxxnsxxysk

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

@aqrln aqrln force-pushed the aqrln-lywxxnsxxysk branch from 00e9c58 to 5dad1ad Compare December 4, 2025 16:47
Handle "invalid text representation"/"invalid input value" (22P02)
errors when using Postgres with pg, neon, or ppg adapters, so that they
are mapped to a proper user facing error.

See this 500 error in QPE that occurred in production: https://prisma-company.slack.com/archives/C04EPL7N2KV/p1764862201609579?thread_ts=1764849204.552389&cid=C04EPL7N2KV

Closes: https://linear.app/prisma-company/issue/TML-1664/fix-incorrect-500-error-returned-from-qpe-instead-of-400
@aqrln aqrln force-pushed the aqrln-lywxxnsxxysk branch from 5dad1ad to 64c9ada Compare December 4, 2025 16:48
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Dec 4, 2025

Client memory tests, node 24, client:
✅ Success

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Dec 4, 2025

size-limit report 📦

Path Size
packages/client/runtime/index-browser.js 2.23 KB (0%)
packages/client/runtime/index-browser.d.ts 3.28 KB (0%)
packages/cli/build/index.js 2.51 MB (0%)
packages/client/prisma-client-0.0.0.tgz 10.23 MB (+0.01% 🔺)
packages/cli/prisma-0.0.0.tgz 6.71 MB (0%)
packages/bundle-size/da-workers-libsql/output.tgz 805.28 KB (+0.01% 🔺)
packages/bundle-size/da-workers-neon/output.tgz 859.2 KB (+0.01% 🔺)
packages/bundle-size/da-workers-pg/output.tgz 852.15 KB (+0.01% 🔺)
packages/bundle-size/da-workers-planetscale/output.tgz 804.72 KB (+0.01% 🔺)
packages/bundle-size/da-workers-d1/output.tgz 788.19 KB (+0.01% 🔺)

jacek-prisma
jacek-prisma previously approved these changes Dec 5, 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.

2 participants