Skip to content

Commit

Permalink
Catch connection errors using transactions (begin) - fixes #162
Browse files Browse the repository at this point in the history
  • Loading branch information
porsager committed Mar 23, 2021
1 parent 16bc7db commit fff6640
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ function Postgres(a, b) {
? 'rollback'
: 'rollback to ' + savepoint
)
.then(() => reject(err))
.then(() => reject(err), reject)
})
.then(begin && (() => {
connections.push(connection)
Expand Down
19 changes: 19 additions & 0 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,25 @@ t('little bobby tables', async() => {
]
})

t('Connection errors are caught using begin()', {
timeout: 20000
}, async() => {
let error
try {
const sql = postgres({ host: 'wat' })

await sql.begin(async(sql) => {
await sql`insert into test (label, value) values (${1}, ${2})`
})

await sql.end()
} catch (err) {
error = err
}

return ['ENOTFOUND', error.code]
})

t('dynamic column name', async() => {
return ['!not_valid', Object.keys((await sql`select 1 as ${ sql('!not_valid') }`)[0])[0]]
})
Expand Down

0 comments on commit fff6640

Please sign in to comment.