Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch errors thrown by connect #3004

Closed
wants to merge 1 commit into from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 11 additions & 10 deletions packages/pg/lib/connection.js
Expand Up @@ -84,19 +84,20 @@ class Connection extends EventEmitter {
socket: self.stream,
}

if (self.ssl !== true) {
Object.assign(options, self.ssl)
try {
if (self.ssl !== true) {
Object.assign(options, self.ssl)

if ('key' in self.ssl) {
options.key = self.ssl.key
if ('key' in self.ssl) {
options.key = self.ssl.key
}
}

var net = require('net')
if (net.isIP && net.isIP(host) === 0) {
options.servername = host
}
}

var net = require('net')
if (net.isIP && net.isIP(host) === 0) {
options.servername = host
}
try {
self.stream = getSecureStream(options)
} catch (err) {
return self.emit('error', err)
Expand Down
13 changes: 13 additions & 0 deletions packages/pg/test/integration/client/error-handling-tests.js
Expand Up @@ -191,6 +191,19 @@ suite.test('when connecting to an invalid host with callback', function (done) {
})
})

suite.test('when connecting with an invalid ssl value', function (done) {
var client = new Client({
ssl: 'off',
})
client.on('error', () => {
assert.fail('unexpected error event when connecting')
})
client.connect(function (error, client) {
assert(error instanceof Error)
done()
})
})

suite.test('when connecting to invalid host with promise', function (done) {
var client = new Client({
user: 'very invalid username',
Expand Down