Skip to content

Commit 13c14f1

Browse files
sehropebrianc
authored andcommitted
fix: Catch and emit query parameter prepareValue(...) errors (brianc#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.
1 parent 566058d commit 13c14f1

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

lib/query.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,12 @@ Query.prototype.prepare = function (connection) {
194194
}
195195

196196
if (self.values) {
197-
self.values = self.values.map(utils.prepareValue)
197+
try {
198+
self.values = self.values.map(utils.prepareValue)
199+
} catch (err) {
200+
this.handleError(err, connection)
201+
return
202+
}
198203
}
199204

200205
// http://developer.postgresql.org/pgdocs/postgres/protocol-flow.html#PROTOCOL-FLOW-EXT-QUERY
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"use strict"
2+
var helper = require('./../test-helper')
3+
4+
const suite = new helper.Suite()
5+
6+
suite.test('Parameter serialization errors should not cause query to hang', (done) => {
7+
if (helper.config.native) {
8+
// pg-native cannot handle non-string parameters so skip this entirely
9+
return done()
10+
}
11+
const client = new helper.pg.Client()
12+
const expectedErr = new Error('Serialization error')
13+
client.connect()
14+
.then(() => {
15+
const obj = {
16+
toPostgres: function () {
17+
throw expectedErr
18+
}
19+
}
20+
return client.query('SELECT $1::text', [obj])
21+
.then(() => {
22+
throw new Error('Expected a serialization error to be thrown but no error was thrown')
23+
})
24+
})
25+
.catch((err) => {
26+
client.end(() => {})
27+
if (err !== expectedErr) {
28+
done(new Error('Expected a serialization error to be thrown but instead caught: ' + err))
29+
return
30+
}
31+
done()
32+
})
33+
})

0 commit comments

Comments
 (0)