Skip to content

Commit 95136c8

Browse files
fix: enable pool error handling
Signed-off-by: Matthew Gabeler-Lee <mgabeler-lee@6river.com>
1 parent f0afde2 commit 95136c8

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

lib/postgresql.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ exports.initialize = function initializeDataSource(dataSource, callback) {
5454
*
5555
* @param {PostgreSQL} postgresql PostgreSQL node.js binding
5656
* @options {Object} settings An object for the data source settings.
57-
* See [node-postgres documentation](https://github.com/brianc/node-postgres/wiki/Client#parameters).
57+
* See [node-postgres documentation](https://node-postgres.com/api/client).
5858
* @property {String} url URL to the database, such as 'postgres://test:mypassword@localhost:5432/devdb'.
5959
* Other parameters can be defined as query string of the url
6060
* @property {String} hostname The host name or ip address of the PostgreSQL DB server
@@ -63,6 +63,8 @@ exports.initialize = function initializeDataSource(dataSource, callback) {
6363
* @property {String} password The password
6464
* @property {String} database The database name
6565
* @property {Boolean} ssl Whether to try SSL/TLS to connect to server
66+
* @property {Function | string} [onError] Optional hook to connect to the pg pool 'error' event,
67+
* or the string 'ignore' to record them with `debug` and otherwise ignore them.
6668
*
6769
* @constructor
6870
*/
@@ -79,6 +81,16 @@ function PostgreSQL(postgresql, settings) {
7981
this.clientConfig.Promise = Promise;
8082
this.pg = new postgresql.Pool(this.clientConfig);
8183

84+
if (settings.onError) {
85+
if (settings.onError === 'ignore') {
86+
this.pg.on('error', function(err) {
87+
debug(err);
88+
});
89+
} else {
90+
this.pg.on('error', settings.onError);
91+
}
92+
}
93+
8294
this.settings = settings;
8395
debug('Settings %j', settings);
8496
}

test/postgresql.initialization.test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,32 @@ describe('postgresql connector errors', function() {
8383
done();
8484
});
8585
});
86+
87+
it('honours onError=ignore', function() {
88+
const settings = newConfig();
89+
settings.onError = 'ignore';
90+
91+
const dataSource = new DataSource(connector, settings);
92+
93+
dataSource.connector.pg.listenerCount('error').should.equal(1);
94+
});
95+
96+
it('honours onError=listener', function() {
97+
const settings = newConfig();
98+
let errorsCaught = 0;
99+
settings.onError = function(err, client) {
100+
err.should.be.ok();
101+
client.should.be.ok();
102+
++errorsCaught;
103+
};
104+
105+
const dataSource = new DataSource(connector, settings);
106+
107+
dataSource.connector.pg.listenerCount('error').should.equal(1);
108+
dataSource.connector.pg.listeners('error').should.deepEqual([settings.onError]);
109+
110+
// send a fake error in and make sure it is bound to our listener
111+
dataSource.connector.pg.emit('error', new Error('fake error'), {fakeClient: true});
112+
errorsCaught.should.equal(1);
113+
});
86114
});

0 commit comments

Comments
 (0)