Skip to content

Errors

pg-schemata exports two custom error classes for structured error handling.

DatabaseError

Wraps PostgreSQL errors thrown by pg-promise with structured metadata.

Import:

js
import { DatabaseError } from 'pg-schemata';
// or
import DatabaseError from 'pg-schemata/src/DatabaseError.js';

Constructor

js
new DatabaseError(message, originalError)
ParameterTypeDescription
messagestringHuman-readable error description
originalErrorErrorThe original PostgreSQL error

Properties

PropertyTypeDescription
namestringAlways 'DatabaseError'
messagestringHuman-readable description
codestringPostgreSQL SQLSTATE error code
detailstringDetailed error message from PostgreSQL
constraintstringName of the violated constraint
tablestringTable where the error occurred
originalErrorThe original PostgreSQL error object

SQLSTATE mapping

The handleDbError method in QueryModel translates common codes:

CodeMessage
23505Unique constraint violation
23503Foreign key constraint violation
23514Check constraint violation
22P02Invalid input syntax for type
OtherDatabase operation failed

SchemaDefinitionError

Thrown for schema validation failures, invalid DTOs, and Zod validation errors.

Import:

js
import { SchemaDefinitionError } from 'pg-schemata';
// or
import SchemaDefinitionError from 'pg-schemata/src/SchemaDefinitionError.js';

Constructor

js
new SchemaDefinitionError(message, originalError?)
ParameterTypeDefaultDescription
messagestringDescription of the schema or validation issue
originalErrorErrornullOptional original error

Properties

PropertyTypeDescription
namestringAlways 'SchemaDefinitionError'
messagestringError description
originalError | nullOriginal error (if provided)
causeZodError | ErrorSet by model methods when Zod validation fails

Common scenarios

ScenarioMessage
Missing primary key in schema'Primary key must be defined in the schema'
Empty DTO on insert'DTO must be a non-empty object'
No valid columns after sanitization'DTO must contain at least one valid column'
Zod validation failure'DTO validation failed' (with .cause containing details)
Invalid WHERE operator'Unsupported operator: $invalid'
Empty conflict columns for upsert'Conflict columns must be a non-empty array'

A lightweight Postgres-first ORM layer.