diff --git a/src/binding.cc b/src/binding.cc index 32b88147..1a432665 100644 --- a/src/binding.cc +++ b/src/binding.cc @@ -13,6 +13,7 @@ using namespace v8; using namespace node; static Persistent connect_symbol; +static Persistent error_symbol; class Connection : public EventEmitter { @@ -30,6 +31,7 @@ public: t->SetClassName(String::NewSymbol("Connection")); connect_symbol = NODE_PSYMBOL("connect"); + error_symbol = NODE_PSYMBOL("error"); NODE_SET_PROTOTYPE_METHOD(t, "connect", Connect); NODE_SET_PROTOTYPE_METHOD(t, "_sendQuery", SendQuery); @@ -275,6 +277,8 @@ private: StopRead(); StopWrite(); LOG("Polled: PGRES_POLLING_FAILED"); + EmitLastError(); + EmitError("Something happened...polling error"); break; case PGRES_POLLING_OK: LOG("Polled: PGRES_POLLING_OK"); @@ -287,6 +291,17 @@ private: } } + void EmitError(const char *message) + { + Local exception = Exception::Error(String::New(message)); + Emit(error_symbol, 1, &exception); + } + + void EmitLastError() + { + EmitError(PQerrorMessage(connection_)); + } + void StopWrite() { TRACE("Stoping write watcher"); diff --git a/test/libpq/binding-spike.js b/test/libpq/binding-spike.js index ff04250f..f0844ba4 100644 --- a/test/libpq/binding-spike.js +++ b/test/libpq/binding-spike.js @@ -15,7 +15,9 @@ test('calling connect without params raises error', function() { test('connecting with wrong parameters', function() { var con = new Connection(); con.connect("user=asldfkj hostaddr=127.0.0.1 port=5432 dbname=asldkfj"); - assert.emits(con, 'error') + assert.emits(con, 'error', function(error) { + console.log(error); + }) });