Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/pg/lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}

Expand Down
9 changes: 9 additions & 0 deletions packages/pg/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -203,4 +211,5 @@ module.exports = {
normalizeQueryConfig,
escapeIdentifier,
escapeLiteral,
ConnectionTimeoutError,
}
18 changes: 11 additions & 7 deletions packages/pg/test/integration/client/connection-timeout-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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))
})
})
Loading