readSqlState
readSqlState(
error):string|undefined
Defined in: packages/server/src/sql-state.ts:22
Read the Postgres SQLSTATE (the five-character error class, e.g. "23505" unique_violation) off a
thrown database error.
Why this helper exists — the bun-sql gotcha. Bun’s built-in SQL/sql driver (what pgxsinkit and
its consumers run against Postgres) surfaces the server’s SQLSTATE on the error’s errno property.
Its code property is bun’s own generic string ("ERR_POSTGRES_SERVER_ERROR"), NOT the SQLSTATE —
so the intuitive error.code read (which worked under postgres.js / pg, where code carried the
SQLSTATE) silently returns the wrong thing under bun-sql. Consumers reaching for a stable, driver-agnostic
SQLSTATE (to map 23505→“already exists”, P0001→a raised app rule, etc.) kept re-deriving this by hand;
this is the one canonical extraction.
Resolution order, returning the first well-formed SQLSTATE found:
errnoon the error (bun-sql),codeon the error (postgres.js / node-postgres, and any driver that follows that convention),- the same two properties walked down the
causechain (a wrapped/re-thrown error).
A “well-formed” SQLSTATE is exactly five characters of [0-9A-Z] — this is what rejects bun’s generic
code string, an errno that is a numeric OS errno, and any other non-SQLSTATE noise. A non-object
error (string, number, null, undefined) yields undefined.
Parameters
Section titled “Parameters”unknown
Returns
Section titled “Returns”string | undefined