Skip to content

Commit a39dfef

Browse files
committed
Ensure non object errors thrown are handled properly
1 parent 26ee478 commit a39dfef

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/connection.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,9 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
385385
}
386386

387387
function queryError(query, err) {
388+
if (!err || typeof err !== 'object')
389+
err = new Error(err)
390+
388391
'query' in err || 'parameters' in err || Object.defineProperties(err, {
389392
stack: { value: err.stack + query.origin.replace(/.*\n/, '\n'), enumerable: options.debug },
390393
query: { value: query.string, enumerable: options.debug },

tests/index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,22 @@ t('Reconnect using SSL', { timeout: 2 }, async() => {
429429
return [1, (await sql`select 1 as x`)[0].x]
430430
})
431431

432+
t('Proper handling of non object Errors', async() => {
433+
const sql = postgres({ socket: () => { throw 'wat' } })
434+
435+
return [
436+
'wat', await sql`select 1 as x`.catch(e => e.message)
437+
]
438+
})
439+
440+
t('Proper handling of null Errors', async() => {
441+
const sql = postgres({ socket: () => { throw null } })
442+
443+
return [
444+
'null', await sql`select 1 as x`.catch(e => e.message)
445+
]
446+
})
447+
432448
t('Login without password', async() => {
433449
return [true, (await postgres({ ...options, ...login })`select true as x`)[0].x]
434450
})

0 commit comments

Comments
 (0)