db/pg_error.ts

PostgreSQL error utilities.

Works with both pg and @electric-sql/pglite — both set .code on error objects using standard PostgreSQL error codes.

view source

Declarations
#

2 declarations

is_pg_unique_violation
#

db/pg_error.ts view source

(error: unknown): boolean import {is_pg_unique_violation} from '@fuzdev/fuz_app/db/pg_error.js';

Check if an error is a PostgreSQL unique constraint violation (error code 23505).

error

the caught error

type unknown

returns

boolean

true if the error is a unique constraint violation

pg_error_code
#

db/pg_error.ts view source

(error: unknown): string | null import {pg_error_code} from '@fuzdev/fuz_app/db/pg_error.js';

Extract the error-code string from a caught error, for SQLSTATE checks.

Other error families also carry string codes (Node FS errors: ENOENT, ENOSPC, …) — compare the result against specific SQLSTATE values, never against truthiness.

error

the caught error

type unknown

returns

string | null

the code string, or null when the value isn't an Error or carries no string code

Imported by
#