Skip to content
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

Sample Error Event Handling Documentation Does Not Work #3202

Open
mnalsup opened this issue Apr 30, 2024 · 0 comments
Open

Sample Error Event Handling Documentation Does Not Work #3202

mnalsup opened this issue Apr 30, 2024 · 0 comments

Comments

@mnalsup
Copy link

mnalsup commented Apr 30, 2024

In the docs here: https://node-postgres.com/features/pooling#checkout-use-and-return

  pool.on('error', (err, client) => {
    console.error('Unexpected error on idle client', err)
    process.exit(-1)
  }

The above sample code snippet did not seem to catch an error from an AWS RDS failover. I received this error when using sample code:

node:events:495
      throw er; // Unhandled 'error' event
      ^

Error: Connection terminated unexpectedly
    at Connection.<anonymous> (/Users/malsup/Development/gitlab.workfront.tech/alloy/fusion-pg/node_modules/pg/lib/client.js:132:73)

However when I added an error event handler to the client in the response from the connect() call as below I was able to catch the error.

const { Pool } = require('pg')

const start = async () => {

  const pool = new Pool({
    user: 'postgres',
    password: 'postgres',
    host: 'localhost',
    port: 5432,
  })

  // the pool will emit an error on behalf of any idle clients
  // it contains if a backend error or network partition happens
  pool.on('error', (err, client) => {
    console.error('Unexpected error on idle client', err)
    process.exit(-1)
  })

  const client = await pool.connect()
  // Added snippet for client errors
  client.on('error', (err, client) => {
    console.error('Unexpected error on idle client', err)
    process.exit(-1)
  })
  for (let i = 0; i < 500; i++) {
    const res = await client.query('SELECT 1')
    console.log(res.rows)
    await new Promise((resolve) => setTimeout(resolve, 500))
  }

  client.release()
}
start()
Unexpected error on idle client Error: Connection terminated unexpectedly
    at Connection.<anonymous> (/Users/malsup/Development/gitlab.workfront.tech/alloy/fusion-pg/node_modules/pg/lib/client.js:132:73)

Is this a documentation issue or should the pool be "forwarding" this error event up from the client?

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

No branches or pull requests

1 participant