Handle expected Prisma request errors - #1365
Conversation
Co-authored-by: Neil Raina <makeitraina@users.noreply.github.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
shouldLogError previously only logged >=500 or unclassified Prisma known errors, so any unexpected error defaulting to 400 (plain Error, TypeError, PrismaClientValidationError, etc.) was returned silently with no log — a loss of Sentry visibility beyond the intended Prisma noise suppression. Invert the check: log unless the error is a recognized expected client error (Zod, Copilot, APIError, or a mapped Prisma known error). Add a test covering an unexpected error still logging. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
Deployment failed with the following error: Learn More: https://vercel.link/multiple-function-regions |
|
Pushed a follow-up commit ( Issue: the original check only logged when Fix: inverted the logic to log unless the error is a recognized expected client error (Zod / Copilot / APIError / a mapped Prisma known error). Zod/Copilot/expected-Prisma stay quiet as intended; anything unrecognized still logs. Added a test asserting an unexpected All 9 tests pass ( |
There was a problem hiding this comment.
Pull request overview
This PR refines the global withErrorHandler utility used by Next.js API routes to treat certain Prisma “known request errors” as expected client failures (returning consistent 404s) while reducing noisy console.error logging for those controlled 4xx responses (helping avoid Sentry noise).
Changes:
- Refactored
withErrorHandlerto normalize errors via helper functions and conditionally log only unexpected/5xx cases. - Added Prisma known-request error mappings for
P2025,P2023, andP2010+ Postgres22P02to return the existing “not found” response without logging. - Added Jest coverage to assert both the Prisma mappings and the logging/no-logging behavior across error classes.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/app/api/core/utils/withErrorHandler.ts | Adds Prisma error normalization + conditional logging to reduce noise while preserving visibility for unexpected errors. |
| src/app/api/tests/utils/withErrorHandler.test.ts | Adds focused unit tests validating Prisma error mapping and console.error behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Greptile flagged that blanket-mapping P2023 ("Inconsistent column data")
to 404 could silently misclassify non-UUID column errors (e.g. enum
mismatches) as not-found and suppress their logging.
Only treat P2023 as not-found when the meta message indicates a malformed
UUID; other P2023 errors now fall through to the unclassified path (400 +
logged). Add a test asserting a non-UUID P2023 stays a logged 400.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
@greptileai review Addressed the P2023 concern from the last review (commit Concern: Fix: Added a test ( |
Changes
withErrorHandler:P2025record-not-foundP2023invalid UUIDP2010raw query failures caused by Postgres22P02invalid UUID castsconsole.errorfor these controlled 4xx responses so they do not create noisy Sentry issues titledPrismaClientKnownRequestError:.Testing Criteria
yarn test src/app/api/tests/utils/withErrorHandler.test.ts --runInBand— 8 tests passed.yarn prettier:check— passed.yarn lint:check— passed with existing React hook/compiler warnings.yarn tsc— blocked by pre-existing missing SVG module declarations insrc/icons/index.tsbefore this change is typechecked.Notes
Impact & Surface Area of Change
withErrorHandler.