diff --git a/packages/pg/lib/client.js b/packages/pg/lib/client.js index 527f62e4f..ae4fb2757 100644 --- a/packages/pg/lib/client.js +++ b/packages/pg/lib/client.js @@ -10,6 +10,7 @@ var Query = require('./query') var defaults = require('./defaults') var Connection = require('./connection') const crypto = require('./crypto/utils') +const { ConnectionTimeoutError } = './utils' class Client extends EventEmitter { constructor(config) { @@ -102,7 +103,7 @@ class Client extends EventEmitter { if (this._connectionTimeoutMillis > 0) { this.connectionTimeoutHandle = setTimeout(() => { con._ending = true - con.stream.destroy(new Error('timeout expired')) + con.stream.destroy(new ConnectionTimeoutError('timeout expired')) }, this._connectionTimeoutMillis) } diff --git a/packages/pg/lib/utils.js b/packages/pg/lib/utils.js index 3bcc4e525..bac1b513c 100644 --- a/packages/pg/lib/utils.js +++ b/packages/pg/lib/utils.js @@ -194,6 +194,14 @@ const escapeLiteral = function (str) { return escaped } +class ConnectionTimeoutError extends Error { + constructor(message) { + super(message) + this.name = 'ConnectionTimeoutError' + this.code = 'CONNECTION_TIMEOUT' + } +} + module.exports = { prepareValue: function prepareValueWrapper(value) { // this ensures that extra arguments do not get passed into prepareValue @@ -203,4 +211,5 @@ module.exports = { normalizeQueryConfig, escapeIdentifier, escapeLiteral, + ConnectionTimeoutError, } diff --git a/packages/pg/test/integration/client/connection-timeout-tests.js b/packages/pg/test/integration/client/connection-timeout-tests.js index 3d6b83664..35737c4a8 100644 --- a/packages/pg/test/integration/client/connection-timeout-tests.js +++ b/packages/pg/test/integration/client/connection-timeout-tests.js @@ -3,7 +3,7 @@ const net = require('net') const buffers = require('../../test-buffers') const helper = require('./test-helper') const assert = require('assert') - +const { ConnectionTimeoutError } = require('../../../lib/utils') const suite = new helper.Suite() const options = { @@ -71,19 +71,23 @@ suite.test('expired connection timeout', (done) => { const opts = { ...options, port: options.port + 1 } serverWithConnectionTimeout(opts.port, opts.connectionTimeoutMillis * 2, (closeServer) => { const timeoutId = setTimeout(() => { - throw new Error('Client should have emitted an error but it did not.') + done(new Error('Client should have emitted an error but it did not.')) }, 3000) const client = new helper.Client(opts) client .connect() - .then(() => client.end()) - .then(() => closeServer(() => done(new Error('Connection timeout should have expired but it did not.')))) + .then(() => { + clearTimeout(timeoutId) + return client + .end() + .then(() => closeServer(() => done(new Error('Connection timeout should have expired but it did not.')))) + }) .catch((err) => { - assert(err instanceof Error) - assert(/timeout expired\s*/.test(err.message)) + clearTimeout(timeoutId) + assert(err instanceof ConnectionTimeoutError) + assert.strictEqual(err.code, 'CONNECTION_TIMEOUT') closeServer(done) }) - .then(() => clearTimeout(timeoutId)) }) })