diff --git a/src/binding.cc b/src/binding.cc index 30627224..8c016134 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 00000000..6619c53f --- /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(); + }) +})