Skip to content

Error thrown on disconnection #4

@chrisdoble

Description

@chrisdoble

If a pg.Client doesn't have an error event listener it throws on connection errors, e.g. if the database restarts (documentation). This causes the process to terminate and prevents it from reconnecting gracefully.

The event listener doesn't need to do anything, e.g. this fixes the issue:

diff --git a/index.js b/index.js
index 3a499ca..b4d9e03 100644
--- a/index.js
+++ b/index.js
@@ -15,6 +15,8 @@ module.exports = (opts) => {
     let testClient
     lastMsg[channel] = Date.now()
     let client = new Client(opts)
+    let onError = (err) => console.error('PostgreSQL client generated an error: ', err);
+    client.on('error', onError);
     let retry = (err) => {
       attempts[channel] = attempts[channel] || 0
       attempts[channel] += 1
@@ -39,6 +41,7 @@ module.exports = (opts) => {
       })
       let checkConnection = () => {
         testClient = new Client(opts)
+        testClient.on('error', onError);
         testClient.connect((err) => {
           if (err) return setTimeout((err) => retry(err), checkInterval)
           testClient.query('SELECT pg_notify($1, $2)', [channel, 'pg-ears-test'], (err) => {

However I didn't raise that in a PR because I don't know what API you'd want.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions