Skip to content

Handle expected Prisma request errors (#1365) - #1367

Merged
priosshrsth merged 3 commits into
productionfrom
main
Jul 2, 2026
Merged

Handle expected Prisma request errors (#1365)#1367
priosshrsth merged 3 commits into
productionfrom
main

Conversation

@priosshrsth

@priosshrsth priosshrsth commented Jul 2, 2026

Copy link
Copy Markdown
Collaborator
  • suppress known prisma error from going to sentry

* Handle expected Prisma request errors
@vercel

vercel Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
tasks-app Ready Ready Preview, Comment Jul 2, 2026 6:23am

Request Review

@priosshrsth
priosshrsth marked this pull request as ready for review July 2, 2026 05:26
@vercel

vercel Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Deployment failed with the following error:

Deploying Serverless Functions to multiple regions is restricted to the Pro and Enterprise plans.

Learn More: https://vercel.link/multiple-function-regions

@greptile-apps

greptile-apps Bot commented Jul 2, 2026

Copy link
Copy Markdown

Greptile Summary

This PR refactors withErrorHandler to classify errors before logging them, replacing a flat console.error on every exception with a tiered approach: unexpected errors still call console.error, but known Prisma errors (P2025, P2023 UUID mismatch, P2010 raw-query invalid input) now produce a console.warn breadcrumb and return a 404, while other expected client errors (Zod, APIError, CopilotApiError) are suppressed entirely.

  • normalizeError centralises response-building logic that was previously inline in the catch block, and shouldLogError / isExpectedPrismaError / isExpectedClientError make the three-way logging decision explicit and testable.
  • A comprehensive test suite covers the P2025, P2023-UUID, P2010, non-UUID P2023, P2002 (unclassified), and generic unexpected-error paths — including verifying that console.warn is called (not console.error) for the mapped Prisma cases.

Confidence Score: 5/5

The change is safe to merge — it only affects logging behaviour and the Prisma error-code mapping; all response status codes and payloads are preserved or improved.

The classification helpers are logically correct for every error branch, the new Prisma error-code paths (P2025, P2023-UUID, P2010) are all exercised by dedicated tests, and the console.warn breadcrumb satisfies the stated goal of keeping a visible trace without triggering Sentry alerts. No incorrect status codes, dropped error data, or broken contracts were found.

No files require special attention.

Important Files Changed

Filename Overview
src/app/api/core/utils/withErrorHandler.ts Refactored error handler with clean classification helpers; logic for all error branches is correct and well-structured
src/app/api/tests/utils/withErrorHandler.test.ts New tests cover all Prisma error variants and verify the warn-vs-error split; existing test assertions updated for the new ZodError message format

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    E([Error thrown]) --> NE[normalizeError]
    NE --> EP{isExpectedPrismaError?}
    EP -- yes --> SL1{shouldLogError?}
    EP -- no --> SL2{shouldLogError?}

    SL1 -- yes\n5xx status --> CERR1[console.error]
    SL1 -- no --> CWARN[console.warn\nbreadcrumb]

    SL2 -- yes\nunexpected / 5xx --> CERR2[console.error]
    SL2 -- no\nZod / APIError / CopilotApiError --> NOLOG[no log]

    CERR1 --> RESP[NextResponse.json]
    CWARN --> RESP
    CERR2 --> RESP
    NOLOG --> RESP
Loading
%%{init: {'theme': 'base', 'themeVariables': {"darkMode": true, "background": "#0d1117", "primaryColor": "#21262d", "primaryTextColor": "#e6edf3", "primaryBorderColor": "#8b949e", "lineColor": "#8b949e", "textColor": "#e6edf3", "edgeLabelBackground": "#161b22", "actorBkg": "#21262d", "actorBorder": "#8b949e", "actorTextColor": "#e6edf3", "actorLineColor": "#8b949e", "signalColor": "#8b949e", "signalTextColor": "#e6edf3", "noteBkgColor": "#373320", "noteBorderColor": "#d4a72c", "noteTextColor": "#f0e6c0", "labelBoxBkgColor": "#21262d", "labelBoxBorderColor": "#8b949e", "labelTextColor": "#e6edf3", "loopTextColor": "#e6edf3", "activationBkgColor": "#30363d", "activationBorderColor": "#8b949e"}}}%%
flowchart TD
    E([Error thrown]) --> NE[normalizeError]
    NE --> EP{isExpectedPrismaError?}
    EP -- yes --> SL1{shouldLogError?}
    EP -- no --> SL2{shouldLogError?}

    SL1 -- yes\n5xx status --> CERR1[console.error]
    SL1 -- no --> CWARN[console.warn\nbreadcrumb]

    SL2 -- yes\nunexpected / 5xx --> CERR2[console.error]
    SL2 -- no\nZod / APIError / CopilotApiError --> NOLOG[no log]

    CERR1 --> RESP[NextResponse.json]
    CWARN --> RESP
    CERR2 --> RESP
    NOLOG --> RESP
Loading

Reviews (2): Last reviewed commit: "Warn on known Prisma errors instead of s..." | Re-trigger Greptile

Comment thread src/app/api/core/utils/withErrorHandler.ts
Comment thread src/app/api/core/utils/withErrorHandler.ts
* Warn on known Prisma errors instead of silencing them
@priosshrsth

Copy link
Copy Markdown
Collaborator Author

@greptileai I addressed your concern. Now we do console.warn on known errors. Re review the pr again.

@priosshrsth
priosshrsth merged commit e5db925 into production Jul 2, 2026
3 checks passed
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