|
1 | | -import { Prisma } from "@trigger.dev/database"; |
| 1 | +import { Prisma, type PrismaClient, isPrismaKnownError } from "@trigger.dev/database"; |
| 2 | +import { logger } from "~/services/logger.server"; |
| 3 | + |
| 4 | +// Minimal structural logger so this stays decoupled from the concrete Logger |
| 5 | +// (and lets tests pass a capturing logger). |
| 6 | +type ErrorLogger = { error: (message: string, fields?: Record<string, unknown>) => void }; |
2 | 7 |
|
3 | 8 | // Prisma connectivity / infrastructure error codes — engine- and |
4 | 9 | // connection-level failures, not query- or validation-level ones. When the |
@@ -37,3 +42,63 @@ export function isInfrastructureError(error: unknown): boolean { |
37 | 42 |
|
38 | 43 | return false; |
39 | 44 | } |
| 45 | + |
| 46 | +// Logs infrastructure failures (P1xxx-class, see isInfrastructureError) and |
| 47 | +// rethrows the ORIGINAL error: callers branch on error.code, and this fires |
| 48 | +// per-statement inside transactions, so converting it would break that. |
| 49 | +export function captureInfrastructureErrors<T extends PrismaClient>( |
| 50 | + client: T, |
| 51 | + log: ErrorLogger = logger |
| 52 | +): T { |
| 53 | + return client.$extends({ |
| 54 | + name: "infrastructure-error-capture", |
| 55 | + query: { |
| 56 | + $allOperations: async ({ model, operation, args, query }) => { |
| 57 | + try { |
| 58 | + return await query(args); |
| 59 | + } catch (error) { |
| 60 | + if (isInfrastructureError(error)) { |
| 61 | + log.error("prisma infrastructure error", { |
| 62 | + model, |
| 63 | + operation, |
| 64 | + code: error instanceof Prisma.PrismaClientKnownRequestError ? error.code : undefined, |
| 65 | + meta: error instanceof Prisma.PrismaClientKnownRequestError ? error.meta : undefined, |
| 66 | + message: error instanceof Error ? error.message : String(error), |
| 67 | + stack: error instanceof Error ? error.stack : undefined, |
| 68 | + }); |
| 69 | + } |
| 70 | + |
| 71 | + throw error; |
| 72 | + } |
| 73 | + }, |
| 74 | + }, |
| 75 | + }) as unknown as T; |
| 76 | +} |
| 77 | + |
| 78 | +// Logs infrastructure errors that reach the $transaction boundary WITHOUT a |
| 79 | +// Prisma error code (e.g. PrismaClientInitializationError). Coded errors are |
| 80 | +// already logged by transac()'s callback, so they are skipped here to avoid |
| 81 | +// double-logging. Returns whether it logged. |
| 82 | +export function logTransactionInfrastructureError( |
| 83 | + error: unknown, |
| 84 | + log: ErrorLogger = logger |
| 85 | +): boolean { |
| 86 | + if (!isInfrastructureError(error) || isPrismaKnownError(error)) { |
| 87 | + return false; |
| 88 | + } |
| 89 | + |
| 90 | + log.error("prisma.$transaction infrastructure error", { |
| 91 | + message: error instanceof Error ? error.message : String(error), |
| 92 | + name: error instanceof Error ? error.name : undefined, |
| 93 | + stack: error instanceof Error ? error.stack : undefined, |
| 94 | + }); |
| 95 | + |
| 96 | + return true; |
| 97 | +} |
| 98 | + |
| 99 | +// Replaces a Prisma infrastructure error's message (which carries the DB |
| 100 | +// hostname) with a generic one before it reaches an API client. Any other |
| 101 | +// error's message is returned unchanged. Status codes/headers are unaffected. |
| 102 | +export function clientSafeErrorMessage(error: Error): string { |
| 103 | + return isInfrastructureError(error) ? "Internal Server Error" : error.message; |
| 104 | +} |
0 commit comments