Skip to content

Commit fff6640

Browse files
committed
Catch connection errors using transactions (begin) - fixes #162
1 parent 16bc7db commit fff6640

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ function Postgres(a, b) {
142142
? 'rollback'
143143
: 'rollback to ' + savepoint
144144
)
145-
.then(() => reject(err))
145+
.then(() => reject(err), reject)
146146
})
147147
.then(begin && (() => {
148148
connections.push(connection)

tests/index.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,25 @@ t('little bobby tables', async() => {
750750
]
751751
})
752752

753+
t('Connection errors are caught using begin()', {
754+
timeout: 20000
755+
}, async() => {
756+
let error
757+
try {
758+
const sql = postgres({ host: 'wat' })
759+
760+
await sql.begin(async(sql) => {
761+
await sql`insert into test (label, value) values (${1}, ${2})`
762+
})
763+
764+
await sql.end()
765+
} catch (err) {
766+
error = err
767+
}
768+
769+
return ['ENOTFOUND', error.code]
770+
})
771+
753772
t('dynamic column name', async() => {
754773
return ['!not_valid', Object.keys((await sql`select 1 as ${ sql('!not_valid') }`)[0])[0]]
755774
})

0 commit comments

Comments
 (0)