Skip to content

Commit da71ea5

Browse files
committed
Add query validity check
Passing nothing for both the query.text and query.name is unsupported but previously crashed with an impossible to catch error.
1 parent bd87cdd commit da71ea5

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

lib/query.js

+6
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,12 @@ Query.prototype.handleError = function(err, connection) {
127127
};
128128

129129
Query.prototype.submit = function(connection) {
130+
if (typeof this.text != 'string' && typeof this.name != 'string') {
131+
const err = new Error('A query must have either text or a name. Supplying neither is unsupported.')
132+
connection.emit('error', err)
133+
connection.emit('readyForQuery')
134+
return
135+
}
130136
if(this.requiresPreparation()) {
131137
this.prepare(connection);
132138
} else {

test/integration/client/error-handling-tests.js

+13
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,16 @@ suite.test('connected, idle client error', (done) => {
159159
client.end(done)
160160
})
161161
})
162+
163+
suite.test('cannot pass non-string values to query as text', (done) => {
164+
const client = new Client()
165+
client.connect()
166+
client.query({ text: { } }, (err) => {
167+
assert(err)
168+
client.query({ }, (err) => {
169+
client.on('drain', () => {
170+
client.end(done)
171+
})
172+
})
173+
})
174+
})

0 commit comments

Comments
 (0)