From 11a4793452d618c53e019416cc886ad38deb1aa7 Mon Sep 17 00:00:00 2001 From: Pat Gaffney Date: Tue, 19 Jun 2018 19:17:13 -0500 Subject: [PATCH] Add error handling for null params to Client.prototype.query() --- lib/client.js | 5 ++++- test/unit/client/simple-query-tests.js | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/client.js b/lib/client.js index 0d023db7c..feedebbd8 100644 --- a/lib/client.js +++ b/lib/client.js @@ -352,7 +352,10 @@ Client.prototype.query = function (config, values, callback) { // can take in strings, config object or query object var query var result - if (typeof config.submit === 'function') { + + if (config === null || config === undefined) { + throw new TypeError('Client was passed a null or undefined query') + } else if (typeof config.submit === 'function') { result = query = config if (typeof values === 'function') { query.callback = query.callback || values diff --git a/test/unit/client/simple-query-tests.js b/test/unit/client/simple-query-tests.js index 363c44dbe..3d1deef41 100644 --- a/test/unit/client/simple-query-tests.js +++ b/test/unit/client/simple-query-tests.js @@ -120,4 +120,24 @@ test('executing query', function () { }) }) }) + + test('handles errors', function () { + var client = helper.client() + + test('throws an error when config is null', function () { + try { + client.query(null, undefined) + } catch (error) { + assert.equal(error.message, 'Client was passed a null or undefined query', 'Should have thrown an Error for null queries') + } + }) + + test('throws an error when config is undefined', function () { + try { + client.query() + } catch (error) { + assert.equal(error.message, 'Client was passed a null or undefined query', 'Should have thrown an Error for null queries') + } + }) + }) })