|
1 | 1 | /** |
2 | | - * Database errors that are the caller's mistake rather than an incident. |
| 2 | + * Recognising a Postgres cast failure, and deciding what to answer for it. |
3 | 3 | * |
4 | | - * One cohesive surface, imported as a unit by the plugin that answers with it |
5 | | - * and by the Sentry plugin that declines to report it. |
| 4 | + * A caller-supplied value that Postgres cannot cast to a column's type raises |
| 5 | + * SQLSTATE **`22P02` (`invalid_text_representation`)**, and until this module |
| 6 | + * existed every one of them surfaced as a `500`. Two are live in production and |
| 7 | + * are the same error class one type apart: |
| 8 | + * |
| 9 | + * - `invalid input value for enum enum_meditations_type: "path"` |
| 10 | + * on `GET /api/meditations?where[type][equals]=path` |
| 11 | + * - `invalid input syntax for type integer: "NaN"` |
| 12 | + * on `GET /api/lectures/daily_fallback_shri_mataji_clip` |
| 13 | + * |
| 14 | + * Pure by design: no Payload types, no request, no logging. `@/plugins/databaseErrors` |
| 15 | + * answers with it on Payload's own REST routes, `@/plugins/sentry` asks it whether an |
| 16 | + * error is a caller's mistake before reporting an incident, and a custom endpoint that |
| 17 | + * catches its own errors calls it directly. The reasoning — why the error is matched |
| 18 | + * rather than the query pre-validated, and what the seam does and does not reach — is in |
| 19 | + * `docs/architecture.md`; the decision that `lesson` (not `Path`) is the in-code name is |
| 20 | + * on sydevs/SahajCloud#670. |
| 21 | + */ |
| 22 | + |
| 23 | +/** SQLSTATE `22P02` — `invalid_text_representation`. */ |
| 24 | +export const INVALID_TEXT_REPRESENTATION = '22P02' |
| 25 | + |
| 26 | +/** How far down the `cause` chain to look. Drizzle adds one level; allow for more. */ |
| 27 | +const MAX_CAUSE_DEPTH = 10 |
| 28 | + |
| 29 | +/** The subset of `pg`'s `DatabaseError` this module reads. */ |
| 30 | +export type PostgresErrorLike = { |
| 31 | + code: string |
| 32 | + message: string |
| 33 | +} |
| 34 | + |
| 35 | +/** What a recognised cast failure should be answered with. */ |
| 36 | +export type MappedClientError = { |
| 37 | + message: string |
| 38 | + status: number |
| 39 | +} |
| 40 | + |
| 41 | +const isPostgresErrorLike = (value: unknown): value is PostgresErrorLike => |
| 42 | + typeof value === 'object' && |
| 43 | + value !== null && |
| 44 | + typeof (value as { code?: unknown }).code === 'string' && |
| 45 | + typeof (value as { message?: unknown }).message === 'string' |
| 46 | + |
| 47 | +/** |
| 48 | + * The first error in `error`'s `cause` chain whose driver `code` is `code`. |
| 49 | + * |
| 50 | + * Drizzle wraps every failed query in a `DrizzleQueryError` (`drizzle-orm/pg-core/session.js`) |
| 51 | + * keeping `pg`'s `DatabaseError` — and with it the SQLSTATE — on `cause`, so the chain is |
| 52 | + * walked rather than the message matched. |
| 53 | + * |
| 54 | + * ⚠ **Match the code; never stop at the first error that carries one.** Anything between |
| 55 | + * Payload and the driver may set a `code` of its own (a `SystemError` such as |
| 56 | + * `ECONNRESET`), and stopping there is silent — no 22P02 found, the 500 comes back. |
| 57 | + * The `ECONNRESET` decoy in `tests/unit/postgres-cast-error.spec.ts` pins it. |
| 58 | + * |
| 59 | + * Returns `null` rather than throwing on a cycle or a non-error — this runs on the error |
| 60 | + * path, where a second failure would replace a bad response with no response at all. |
| 61 | + */ |
| 62 | +export function findPostgresError(error: unknown, code: string): null | PostgresErrorLike { |
| 63 | + const seen = new Set<unknown>() |
| 64 | + let current = error |
| 65 | + |
| 66 | + for (let depth = 0; depth <= MAX_CAUSE_DEPTH; depth++) { |
| 67 | + if (current === null || typeof current !== 'object' || seen.has(current)) return null |
| 68 | + if (isPostgresErrorLike(current) && current.code === code) return current |
| 69 | + seen.add(current) |
| 70 | + current = (current as { cause?: unknown }).cause |
| 71 | + } |
| 72 | + |
| 73 | + return null |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * `{ status, message }` when this error is a Postgres cast failure, `null` otherwise — in |
| 78 | + * which case nothing about the error's handling changes. |
| 79 | + * |
| 80 | + * The message names the offending value because Postgres does, and that is the whole |
| 81 | + * point of the 400: the caller can see what it sent. It names the Postgres *type* |
| 82 | + * (`enum_meditations_type`) rather than the field, because SQLSTATE 22P02 carries the |
| 83 | + * type and not the column — a worse-worded 400 that cannot miss a case, rather than a |
| 84 | + * better-worded one that only covers enums. |
6 | 85 | */ |
| 86 | +export function mapPostgresCastError(error: unknown): MappedClientError | null { |
| 87 | + const postgresError = findPostgresError(error, INVALID_TEXT_REPRESENTATION) |
| 88 | + if (!postgresError) return null |
7 | 89 |
|
8 | | -export type { MappedClientError, PostgresErrorLike } from './postgresCastError' |
9 | | -export { |
10 | | - findPostgresError, |
11 | | - INVALID_TEXT_REPRESENTATION, |
12 | | - mapPostgresCastError, |
13 | | -} from './postgresCastError' |
| 90 | + return { |
| 91 | + message: `Invalid value in request: ${postgresError.message}`, |
| 92 | + status: 400, |
| 93 | + } |
| 94 | +} |
0 commit comments