From 4f4cb7539e138af522ade6584790d0c50858d99b Mon Sep 17 00:00:00 2001 From: Pratik Dulal Date: Tue, 18 Aug 2026 22:57:57 +0545 Subject: [PATCH] fix: add default error listeners to clients and pools --- packages/pg-pool/index.js | 1 + packages/pg-pool/test/events.js | 5 +++++ packages/pg/lib/client.js | 1 + packages/pg/lib/native/client.js | 1 + packages/pg/test/unit/client/configuration-tests.js | 7 +++++++ 5 files changed, 15 insertions(+) diff --git a/packages/pg-pool/index.js b/packages/pg-pool/index.js index ab514fa88..32d1a4000 100644 --- a/packages/pg-pool/index.js +++ b/packages/pg-pool/index.js @@ -66,6 +66,7 @@ function makeIdleListener(pool, client) { class Pool extends EventEmitter { constructor(options, Client) { super() + this.on('error', NOOP) this.options = Object.assign({}, options) if (options != null && 'password' in options) { diff --git a/packages/pg-pool/test/events.js b/packages/pg-pool/test/events.js index 809c2159a..68576c740 100644 --- a/packages/pg-pool/test/events.js +++ b/packages/pg-pool/test/events.js @@ -7,6 +7,11 @@ const it = require('mocha').it const Pool = require('../') describe('events', function () { + it('does not throw when an error has no listener', function () { + const pool = new Pool() + expect(() => pool.emit('error', new Error('problem'))).not.to.throwError() + }) + it('emits connect before callback', function (done) { const pool = new Pool() let emittedClient = false diff --git a/packages/pg/lib/client.js b/packages/pg/lib/client.js index 2b13c1de7..df4207fc8 100644 --- a/packages/pg/lib/client.js +++ b/packages/pg/lib/client.js @@ -50,6 +50,7 @@ function coerceNumberOrDefault(value, defaultValue) { class Client extends EventEmitter { constructor(config) { super() + this.on('error', () => {}) this.connectionParameters = new ConnectionParameters(config) this.user = this.connectionParameters.user diff --git a/packages/pg/lib/native/client.js b/packages/pg/lib/native/client.js index 9ec3c8c03..3775edf42 100644 --- a/packages/pg/lib/native/client.js +++ b/packages/pg/lib/native/client.js @@ -22,6 +22,7 @@ const queryQueueLengthDeprecationNotice = nodeUtils.deprecate( const Client = (module.exports = function (config) { EventEmitter.call(this) + this.on('error', () => {}) config = config || {} this._Promise = config.Promise || global.Promise diff --git a/packages/pg/test/unit/client/configuration-tests.js b/packages/pg/test/unit/client/configuration-tests.js index 63d4ea649..14ed44729 100644 --- a/packages/pg/test/unit/client/configuration-tests.js +++ b/packages/pg/test/unit/client/configuration-tests.js @@ -60,6 +60,13 @@ test('client settings', function () { }) }) +test('emitting an error without a listener does not throw', function () { + const client = new Client() + assert.doesNotThrow(function () { + client.emit('error', new Error('expected')) + }) +}) + test('initializing from a config string', function () { test('uses connectionString property', function () { const client = new Client({