Skip to content

Commit 869ab9b

Browse files
committed
Fix connection config errors being swallowed
1 parent 3aacc40 commit 869ab9b

File tree

2 files changed

+38
-22
lines changed

2 files changed

+38
-22
lines changed

lib/connection.js

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function Connection(options = {}) {
3838
, connection = { send, end, destroy }
3939

4040
const socket = postgresSocket(options, {
41-
ready: () => socket.write(frontend.connect(options)),
41+
ready,
4242
data,
4343
error,
4444
close
@@ -114,26 +114,26 @@ function Connection(options = {}) {
114114
}
115115

116116
function send(query, { sig, str, args = [] }) {
117-
query.str = str
118-
query.args = args
119-
query.result = []
120-
query.result.count = null
121-
idle_timeout && clearTimeout(timer)
122-
123-
typeof options.debug === 'function' && options.debug(id, str, args)
124-
const buffer = query.simple
125-
? simple(str, query)
126-
: sig in statements
127-
? prepared(statements[sig], args, query)
128-
: prepare(sig, str, args, query)
129-
130-
ready
131-
? (backend.query = query, ready = false)
132-
: queries.push(query)
133-
134-
open
135-
? socket.write(buffer)
136-
: (messages.push(buffer), connect())
117+
query.str = str
118+
query.args = args
119+
query.result = []
120+
query.result.count = null
121+
idle_timeout && clearTimeout(timer)
122+
123+
typeof options.debug === 'function' && options.debug(id, str, args)
124+
const buffer = query.simple
125+
? simple(str, query)
126+
: sig in statements
127+
? prepared(statements[sig], args, query)
128+
: prepare(sig, str, args, query)
129+
130+
ready
131+
? (backend.query = query, ready = false)
132+
: queries.push(query)
133+
134+
open
135+
? socket.write(buffer)
136+
: (messages.push(buffer), connect())
137137
}
138138

139139
function connect() {
@@ -244,7 +244,6 @@ function Connection(options = {}) {
244244
function postgresSocket(options, {
245245
error,
246246
close,
247-
ready,
248247
data
249248
}) {
250249
let socket
@@ -295,6 +294,14 @@ function postgresSocket(options, {
295294
socket.once('close', onclose)
296295
}
297296

297+
function ready() {
298+
try {
299+
socket.write(frontend.connect(options))
300+
} catch (e) {
301+
error(e)
302+
}
303+
}
304+
298305
const x = {
299306
write: x => {
300307
buffer = buffer ? Buffer.concat([buffer, x]) : Buffer.from(x)

tests/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,3 +1075,12 @@ t('no_prepare: true disables prepared transactions', async() => {
10751075
const result = await sql`select * from pg_prepared_statements`
10761076
return [0, result.count]
10771077
})
1078+
1079+
o('Catches connection config errors', async() => {
1080+
const sql = postgres({ user: { toString: () => { throw new Error('wat') } }, database: 'prut' })
1081+
1082+
return [
1083+
'wat',
1084+
await sql`select 1`.catch((e) => e.message)
1085+
]
1086+
})

0 commit comments

Comments
 (0)