Skip to content

fix(drizzle): reject initializing with the connect error and handle it - #18658

Open
Metehan-Bicer wants to merge 1 commit into
payloadcms:mainfrom
Metehan-Bicer:fix/postgres-initializing-rejection
Open

Metehan-Bicer wants to merge 1 commit into
payloadcms:mainfrom
Metehan-Bicer:fix/postgres-initializing-rejection

Conversation

@Metehan-Bicer

Copy link
Copy Markdown
Contributor

What?

When connect() fails on a SQL adapter, it no longer leaves an unhandledRejection with reason undefined behind. The adapter's initializing promise now rejects with the connect error and has a handler from the moment it is created.

Why?

As described in #18656: connect() called rejectInitializing() with no argument and then threw its own error. Only beginTransaction awaits initializing, so on a request path that opens no transaction nothing handles that rejection. Node reports it as an unhandled rejection whose reason is undefined, with no stack and nothing that points at Payload or the database. The same call is in db-postgres, db-vercel-postgres, db-sqlite and db-d1-sqlite, and destroy creates a fresh initializing the same way.

How?

Both suggestions from the issue, in all four adapters:

  • connect() passes the error, rejectInitializing(err), and the type is widened to (err?: unknown) => void in drizzle's shared types and each adapter's.
  • A no-op handler is attached where initializing is created (each adapter's index.ts and drizzle/src/destroy.ts). connect() still throws the real error and beginTransaction's own await still rejects, so nothing is swallowed.

Tests: packages/db-postgres/src/connect.spec.ts (new) builds the adapter with a pg stub whose Pool.connect rejects, then checks that no unhandled rejection is reported and that initializing rejects with the connect error. It fails on main (the listener receives undefined) and passes with the change. The other three adapters get the identical change; the spec covers db-postgres. test/database passes on Postgres and SQLite, and the drizzle and adapter unit specs pass.

With the real driver, getPayload with postgresAdapter pointed at a port with nothing listening:

# main
getPayload threw -> Error: cannot connect to Postgres: connect ECONNREFUSED 127.0.0.1:1
unhandledRejection -> typeof reason: undefined | isError: false
# this PR
getPayload threw -> Error: cannot connect to Postgres: connect ECONNREFUSED 127.0.0.1:1

Fixes #18656

connect() rejected the adapter's `initializing` promise with no argument
and then threw its own error. Only beginTransaction awaits that promise,
so a failed connect on a path that opens no transaction surfaced as an
unhandledRejection with reason `undefined`. The four SQL adapters now pass
the connect error, and a no-op handler is attached wherever the promise
is created; connect() still throws and beginTransaction still rejects.

This branch has not been deployed

No deployments
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.

db-postgres: connect() rejects its initializing promise with no argument, producing an unhandledRejection with reason undefined

1 participant