Skip to content

Conversation

@pyramation
Copy link
Contributor

No description provided.

- Add shared pg-error-format utility in @pgpmjs/types with extractPgErrorFields,
  formatPgErrorFields, and formatPgError functions
- Enhance pgpm/core migrate/utils/transaction.ts with extended PG error fields
- Enhance pgpm/core migrate/client.ts with extended PG error fields
- Add opt-in enhanced errors to PgTestClient via enhancedErrors option or
  PGSQL_TEST_ENHANCED_ERRORS env var
- Improve seed error handling in pgsql-test connect.ts
- Add comprehensive tests for error formatting utilities

This provides better debugging information for PostgreSQL errors including:
- detail, hint, where, position fields
- schema, table, column, dataType, constraint fields
- query and values context when available
Enhanced PostgreSQL error messages are now enabled by default for better
debugging experience. Can be disabled via enhancedErrors: false option or
PGSQL_TEST_ENHANCED_ERRORS=0 environment variable.
- Remove PGSQL_TEST_ENHANCED_ERRORS env var check (enhanced errors now
  always default to true, can be disabled via enhancedErrors: false option)
- Add tests for nested EXECUTE migration errors with full call stack context
- Add tests for constraint violations in nested EXECUTE
- Add tests for transaction aborted errors with context
Add Jest snapshots showing the exact formatted output for:
1. JSON/JSONB type mismatch error (simple case)
2. Nested EXECUTE migration error with full PL/pgSQL call stack
Remove all mock error objects and replace with real database tests that:
- Create actual tables with constraints
- Trigger real PostgreSQL errors (JSON type mismatch, unique violations, FK violations, etc.)
- Use getConnections() and PgTestClient for proper test isolation
- Include snapshot tests for error message formatting

Tests will generate snapshots in CI where PostgreSQL is available.
Snapshots generated from actual PostgreSQL errors in CI:
1. JSON/JSONB type mismatch error
2. Unique constraint violation error
3. Foreign key violation error
4. Undefined table error
5. Nested EXECUTE error with PL/pgSQL call stack
6. Constraint violation inside nested EXECUTE
…cedures

Use GET STACKED DIAGNOSTICS to capture all error fields (sqlstate, message,
detail, hint, context, schema, table, column, constraint, datatype) when
EXECUTE fails, and re-raise with RAISE EXCEPTION USING to preserve them.

This ensures the Node.js pg library receives the full error context, which
can then be formatted by formatPgError for enhanced error messages.
The GET STACKED DIAGNOSTICS fix now preserves PostgreSQL error context:
- code: error codes like 42P01, 23505, 22P02
- detail: constraint violation details
- schema/table/constraint: object identifiers
- where: full PL/pgSQL call stack
- internalQuery: the actual failing SQL statement
@pyramation pyramation merged commit 40b07e7 into main Jan 2, 2026
36 checks passed
@pyramation pyramation deleted the feat/improved-errors branch January 2, 2026 04:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants