Skip to content

Commit 4f4cb75

Browse files
committed
fix: add default error listeners to clients and pools
1 parent c9e5761 commit 4f4cb75

5 files changed

Lines changed: 15 additions & 0 deletions

File tree

packages/pg-pool/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ function makeIdleListener(pool, client) {
6666
class Pool extends EventEmitter {
6767
constructor(options, Client) {
6868
super()
69+
this.on('error', NOOP)
6970
this.options = Object.assign({}, options)
7071

7172
if (options != null && 'password' in options) {

packages/pg-pool/test/events.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ const it = require('mocha').it
77
const Pool = require('../')
88

99
describe('events', function () {
10+
it('does not throw when an error has no listener', function () {
11+
const pool = new Pool()
12+
expect(() => pool.emit('error', new Error('problem'))).not.to.throwError()
13+
})
14+
1015
it('emits connect before callback', function (done) {
1116
const pool = new Pool()
1217
let emittedClient = false

packages/pg/lib/client.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ function coerceNumberOrDefault(value, defaultValue) {
5050
class Client extends EventEmitter {
5151
constructor(config) {
5252
super()
53+
this.on('error', () => {})
5354

5455
this.connectionParameters = new ConnectionParameters(config)
5556
this.user = this.connectionParameters.user

packages/pg/lib/native/client.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const queryQueueLengthDeprecationNotice = nodeUtils.deprecate(
2222

2323
const Client = (module.exports = function (config) {
2424
EventEmitter.call(this)
25+
this.on('error', () => {})
2526
config = config || {}
2627

2728
this._Promise = config.Promise || global.Promise

packages/pg/test/unit/client/configuration-tests.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ test('client settings', function () {
6060
})
6161
})
6262

63+
test('emitting an error without a listener does not throw', function () {
64+
const client = new Client()
65+
assert.doesNotThrow(function () {
66+
client.emit('error', new Error('expected'))
67+
})
68+
})
69+
6370
test('initializing from a config string', function () {
6471
test('uses connectionString property', function () {
6572
const client = new Client({

0 commit comments

Comments
 (0)