From f4ca716b934a9b11680f70d2735ee681b616ec10 Mon Sep 17 00:00:00 2001 From: brianc Date: Thu, 24 Feb 2011 21:44:03 -0600 Subject: [PATCH] throw exception when passing non-string to query --- src/binding.cc | 5 ++++- test/native/error-tests.js | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 test/native/error-tests.js diff --git a/src/binding.cc b/src/binding.cc index 3062722..8c01613 100644 --- a/src/binding.cc +++ b/src/binding.cc @@ -79,8 +79,11 @@ public: { HandleScope scope; Connection *self = ObjectWrap::Unwrap(args.This()); - String::Utf8Value queryText(args[0]->ToString()); + if(!args[0]->IsString()) { + return ThrowException(Exception::Error(String::New("First parameter must be a string query"))); + } + String::Utf8Value queryText(args[0]->ToString()); int result = self->Send(*queryText); if(result == 0) { THROW("PQsendQuery returned error code"); diff --git a/test/native/error-tests.js b/test/native/error-tests.js new file mode 100644 index 0000000..6619c53 --- /dev/null +++ b/test/native/error-tests.js @@ -0,0 +1,18 @@ +var helper = require(__dirname + "/../test-helper"); +var Client = require(__dirname + "/../../lib/native").Client; +var conString = helper.connectionString(); + +test('query with non-text as first parameter throws error', function() { + var client = new Client(conString); + client.connect(); + assert.emits(client, 'connect', function() { + var err; + try{ + client.query({text:{fail: true}}); + } catch(e) { + err = e; + } + assert.ok(err != null, "Expected exception to be thrown") + client.end(); + }) +})