Skip to content

feat: add codes to errors created by pg and pg-pool - #3849

Open
oyobbeb wants to merge 1 commit into
brianc:masterfrom
oyobbeb:feat-client-error-codes
Open

oyobbeb wants to merge 1 commit into
brianc:masterfrom
oyobbeb:feat-client-error-codes

Conversation

@oyobbeb

@oyobbeb oyobbeb commented Nov 4, 2026 •

Copy link
Copy Markdown

PostgreSQL server errors expose a SQLSTATE through err.code, but errors created by the client—such as timeouts, connection termination, and SSL negotiation failures—lack codes. Consumers therefore have to match error messages to handle these cases (#3380).

This PR adds ERR_PG_ codes to these runtime errors without changing their messages or error types.

Code Message
ERR_PG_CONNECTION_TIMEOUT timeout expired
ERR_PG_CONNECTION_TERMINATED Connection terminated unexpectedly
ERR_PG_CONNECTION_ENDED Connection terminated
ERR_PG_QUERY_TIMEOUT Query read timeout
ERR_PG_SSL_NOT_SUPPORTED The server does not support SSL connections
ERR_PG_SSL_NEGOTIATION_FAILED There was an error establishing an SSL connection
ERR_PG_POOL_CONNECT_TIMEOUT Connection terminated due to connection timeout
ERR_PG_POOL_CHECKOUT_TIMEOUT timeout exceeded when trying to connect

The naming follows the Node-style ERR_PG_… convention discussed in #2498 and does not overlap with server SQLSTATEs. Following the same review guidance, API misuse errors, such as using an ended pool or calling end() twice, remain unchanged.

In the native client, only query timeouts and the Connection terminated error from end() receive these codes. Connection and SSL errors come from libpq and remain without a code. The client and pool documentation now include error-code tables.

Compatibility

Although messages and error types remain unchanged, consumers that identify server errors using typeof err.code === 'string' may behave differently.

For example, Prisma's adapter-pg checks an allowlist of TLS codes when code is a string. With this change, the two SSL errors above are no longer classified as P1011, as verified with Prisma 7.8.0. If this PR is merged, I plan to submit a follow-up PR to Prisma adding both codes to that list.

An existing pg-pool test also assumed that any error with a code must be ECONNRESET; this PR updates that assertion.

Testing

  • Added code assertions to existing tests and a new pool regression test.
  • The regression tests fail on master and pass with this change.
  • The full yarn test suite passes on Linux with Node.js 24 and PostgreSQL 17, including tests using the native bindings.

Closes #3380.

Errors from the server carry the SQLSTATE in `code`, but errors that the
client creates itself (connection and query timeouts, a terminated
connection, SSL refusal) had none, so callers had to match messages.

Give the runtime ones an `ERR_PG_` code and document them. Messages stay
the same. Errors for library misuse are left without a code.

Closes brianc#3380
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.

Feature: Throw proper errors

1 participant