Repository navigation
Improve CLI error handling for missing database credentials - #6
Merged
Merged
Conversation
Previously, running 'dbh migrate' or 'dbh reverse' without the required POSTGRES_* environment variables dumped a raw stack trace that only mentioned the first missing variable, forcing users to fix them one at a time. - parseDatabaseCredentials now collects every missing required credential (POSTGRES_HOST, POSTGRES_DATABASE, POSTGRES_USER, POSTGRES_PASSWORD) and throws a single MissingDatabaseCredentialsError listing all of them - The CLI's migrate/reverse commands catch adapter-initialization errors and print a clean, actionable message: which variables are missing vs set, which are optional, and how to provide them (env vars or --env-file), with no stack trace - A missing --env-file path now also exits with a clean error message - MissingDatabaseCredentialsError and REQUIRED_POSTGRES_CREDENTIAL_KEYS are exported from the package root so consumers can catch it - Add unit tests for the aggregated error and the CLI failure modes Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01RDMoz2eWFyzx5rry6i739k
Make POSTGRES_URL required
Credential parsing previously required POSTGRES_HOST, POSTGRES_DATABASE, POSTGRES_USER, and POSTGRES_PASSWORD even when a full POSTGRES_URL connection string was provided. Credentials are now satisfied by EITHER a POSTGRES_URL (host, database, user, password, and port are derived from it for any component not set individually) OR the four component variables (from which POSTGRES_URL is constructed, as before). - Add parsePostgresUrl util: extracts components from postgres:// or postgresql:// URLs, decoding percent-encoded userinfo; throws a descriptive error for malformed URLs, but only when the URL is actually needed to fill missing components (a pass-through connection string with fully-specified component variables still works) - Individually-set variables take precedence over URL components; port precedence is POSTGRES_PORT > port in POSTGRES_URL > 5432, and a numeric POSTGRES_PORT in a credentials object is now honored - Fix unit tests broken by adding POSTGRES_URL to REQUIRED_POSTGRES_CREDENTIAL_KEYS: parseDatabaseCredentials now resolves that key, and MissingDatabaseCredentialsError explains the either/or requirement while still listing every unresolved key - Update the CLI missing-credentials message to present both options with per-variable (set)/(missing) status - Relax BaseInitializablePostgresDatabaseCredentials so URL-only credentials objects typecheck; export the new component-keys constant - Extend unit tests: URL-only parsing, precedence, incomplete URLs, malformed URLs, scheme validation, and adapter init from URL only Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01RDMoz2eWFyzx5rry6i739k
- package.json: 0.12.1 -> 0.13.0 - README: update the postgres-ws-proxy image version example to match Also correct the README's "Required Environment Variables" section, which still described all POSTGRES_* component variables as required. Credentials are now satisfied by either POSTGRES_URL alone or the four component variables, so document both options, how they merge, and move POSTGRES_PORT / POSTGRES_URL_NON_POOLING into the optional section (merging the two optional-variable blocks into one). Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01RDMoz2eWFyzx5rry6i739k
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
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.
Summary
Refactored database credential validation to provide clear, actionable error messages when required environment variables are missing. Instead of failing on the first missing variable with a generic error, the CLI now reports all missing credentials at once and guides users on how to provide them.
Key Changes
New error class (
MissingDatabaseCredentialsError): Captures all missing required credentials at once rather than failing on the first one. Includes amissingproperty listing every absent key.Refactored credential parsing (
parseDatabaseCredentials): Collects all required credentials before validation, then throwsMissingDatabaseCredentialsErrorwith the complete list of missing keys if any are absent.Enhanced CLI error handling:
createCliDbhOrExit()function catchesMissingDatabaseCredentialsErrorand prints a formatted table showing which credentials are missing vs. set, along with guidance on how to provide them (environment variables or--env-file).loadCliEnvFileOrExit()function wraps env file loading with clean error messages instead of stack traces.migrateandreversecommands now use these helpers.Comprehensive test coverage:
parseDatabaseCredentialsvalidating error behavior with partial/complete missing credentials.Implementation Details
REQUIRED_POSTGRES_CREDENTIAL_KEYSconstant defines the four mandatory credentials:POSTGRES_HOST,POSTGRES_DATABASE,POSTGRES_USER,POSTGRES_PASSWORD.POSTGRES_PORT,POSTGRES_URL,POSTGRES_URL_NON_POOLING) are handled separately and do not trigger errors when absent.MissingDatabaseCredentialsErrorprogrammatically.https://claude.ai/code/session_01RDMoz2eWFyzx5rry6i739k