-
Notifications
You must be signed in to change notification settings - Fork 5
feat: improve PostgreSQL error messages with extended fields #544
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- 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
Contributor
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
feat: improve PostgreSQL error messages with extended fields
Summary
Adds comprehensive PostgreSQL error formatting utilities that surface extended error fields (detail, hint, where, position, schema, table, column, constraint, etc.) for better debugging. These fields are available from pg-protocol but were previously not surfaced in error messages.
Changes:
@pgpmjs/typeswithextractPgErrorFields,formatPgErrorFields, andformatPgErrorfunctionspgpm/coremigration utilities (transaction.ts, client.ts)PgTestClientenabled by default (can be disabled viaenhancedErrors: false)GET STACKED DIAGNOSTICSPgpmMigrate.deploy()andrevert()with extended fieldsExample enhanced error output for JSON/JSONB type mismatch:
Example nested EXECUTE migration error (captures full PL/pgSQL call stack):
Updates since last revision
pgpm_migrate.deployandpgpm_migrate.revertinprocedures.sqlto useGET STACKED DIAGNOSTICSto capture all error fields (sqlstate, message, detail, hint, context, schema, table, column, constraint, datatype) and re-raise withRAISE EXCEPTION USINGto preserve themdeploy()andrevert()now callformatPgError()on caught errors before re-throwing, with a__pgpmEnhancedguard flag to prevent double-formattingundefinedReview & Testing Checklist for Human
pgpm/core/src/migrate/sql/procedures.sql- theGET STACKED DIAGNOSTICSandRAISE EXCEPTION USINGpattern is used to preserve error context. Verify this is compatible with your PostgreSQL version (tested on PG 13, should work on PG 9.1+)client.tsmutateserror.messagedirectly and adds__pgpmEnhancedflag. This preserves instanceof but modifies the original error objectpostgres-test.pgpm-migration-errors.test.ts.snap- errors should now show code, detail, where, schema, table, constraint fields populatedtransaction.tsnow logs full params instead of truncating to 3 items. Verify this is acceptable for your use case (could expose sensitive data in logs)Recommended test plan:
where,detail,schema,tablewherefield shows the full PL/pgSQL call stack throughpgpm_migrate.deployNotes
GET STACKED DIAGNOSTICSapproach is version-independent (PG 9.1+) - the root cause was PL/pgSQL catching/rethrowing aroundEXECUTEwhich discards extended fields unless explicitly capturedLink to Devin run: https://app.devin.ai/sessions/6a6a51e6869745d390b2f36f18ebfdb8
Requested by: Dan Lynch (@pyramation)