From 13c14f1de0198952914bab2c9ba1613bea6f7371 Mon Sep 17 00:00:00 2001 From: Sehrope Sarkuni Date: Tue, 16 Apr 2019 18:29:40 -0400 Subject: [PATCH] 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. --- lib/query.js | 7 ++++- test/integration/gh-issues/1854-tests.js | 33 ++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 test/integration/gh-issues/1854-tests.js diff --git a/lib/query.js b/lib/query.js index 9e34b0d2..41ed77ea 100644 --- a/lib/query.js +++ b/lib/query.js @@ -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 diff --git a/test/integration/gh-issues/1854-tests.js b/test/integration/gh-issues/1854-tests.js new file mode 100644 index 00000000..8dbe37ab --- /dev/null +++ b/test/integration/gh-issues/1854-tests.js @@ -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() + }) +})