Skip to content

Commit

Permalink
Fix connection config errors being swallowed
Browse files Browse the repository at this point in the history
  • Loading branch information
porsager committed Sep 29, 2020
1 parent 3aacc40 commit 869ab9b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 22 deletions.
51 changes: 29 additions & 22 deletions lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function Connection(options = {}) {
, connection = { send, end, destroy }

const socket = postgresSocket(options, {
ready: () => socket.write(frontend.connect(options)),
ready,
data,
error,
close
Expand Down Expand Up @@ -114,26 +114,26 @@ function Connection(options = {}) {
}

function send(query, { sig, str, args = [] }) {
query.str = str
query.args = args
query.result = []
query.result.count = null
idle_timeout && clearTimeout(timer)

typeof options.debug === 'function' && options.debug(id, str, args)
const buffer = query.simple
? simple(str, query)
: sig in statements
? prepared(statements[sig], args, query)
: prepare(sig, str, args, query)

ready
? (backend.query = query, ready = false)
: queries.push(query)

open
? socket.write(buffer)
: (messages.push(buffer), connect())
query.str = str
query.args = args
query.result = []
query.result.count = null
idle_timeout && clearTimeout(timer)

typeof options.debug === 'function' && options.debug(id, str, args)
const buffer = query.simple
? simple(str, query)
: sig in statements
? prepared(statements[sig], args, query)
: prepare(sig, str, args, query)

ready
? (backend.query = query, ready = false)
: queries.push(query)

open
? socket.write(buffer)
: (messages.push(buffer), connect())
}

function connect() {
Expand Down Expand Up @@ -244,7 +244,6 @@ function Connection(options = {}) {
function postgresSocket(options, {
error,
close,
ready,
data
}) {
let socket
Expand Down Expand Up @@ -295,6 +294,14 @@ function postgresSocket(options, {
socket.once('close', onclose)
}

function ready() {
try {
socket.write(frontend.connect(options))
} catch (e) {
error(e)
}
}

const x = {
write: x => {
buffer = buffer ? Buffer.concat([buffer, x]) : Buffer.from(x)
Expand Down
9 changes: 9 additions & 0 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1075,3 +1075,12 @@ t('no_prepare: true disables prepared transactions', async() => {
const result = await sql`select * from pg_prepared_statements`
return [0, result.count]
})

o('Catches connection config errors', async() => {
const sql = postgres({ user: { toString: () => { throw new Error('wat') } }, database: 'prut' })

return [
'wat',
await sql`select 1`.catch((e) => e.message)
]
})

0 comments on commit 869ab9b

Please sign in to comment.