fix: Catch and emit query parameter prepareValue(...) errors (#1855)

Adds a try/catch block around the prepareValue(...) invocations in query.prepare(...)
to ensure that any that throw an error are caught and bubbled up to the caller.
This commit is contained in:
Sehrope Sarkuni 2019-04-16 18:29:40 -04:00 committed by Brian C
parent 566058de17
commit 13c14f1de0
2 changed files with 39 additions and 1 deletions

View File

@ -194,7 +194,12 @@ Query.prototype.prepare = function (connection) {
}
if (self.values) {
self.values = self.values.map(utils.prepareValue)
try {
self.values = self.values.map(utils.prepareValue)
} catch (err) {
this.handleError(err, connection)
return
}
}
// http://developer.postgresql.org/pgdocs/postgres/protocol-flow.html#PROTOCOL-FLOW-EXT-QUERY

View File

@ -0,0 +1,33 @@
"use strict"
var helper = require('./../test-helper')
const suite = new helper.Suite()
suite.test('Parameter serialization errors should not cause query to hang', (done) => {
if (helper.config.native) {
// pg-native cannot handle non-string parameters so skip this entirely
return done()
}
const client = new helper.pg.Client()
const expectedErr = new Error('Serialization error')
client.connect()
.then(() => {
const obj = {
toPostgres: function () {
throw expectedErr
}
}
return client.query('SELECT $1::text', [obj])
.then(() => {
throw new Error('Expected a serialization error to be thrown but no error was thrown')
})
})
.catch((err) => {
client.end(() => {})
if (err !== expectedErr) {
done(new Error('Expected a serialization error to be thrown but instead caught: ' + err))
return
}
done()
})
})