From 1551f1d183579f4446d9b7b683275f64813c0521 Mon Sep 17 00:00:00 2001 From: Ben Montgomery Date: Tue, 24 Jul 2012 12:53:00 -0400 Subject: [PATCH 1/2] Improve error message accuracy of native SendQuery * add private method const char *GetLastError() to src/binding.cc * add var const char *lastErrorMessage to SendQuery in src/binding.cc * throw result of PQErrorMessage inside SendQuery This commit replaces the static/vague error message "PQsendQuery returned error code" with the actual message text of the error. The method GetLastError() returns the text of PQErrorMessage. GetLastError is called from within SendQuery to retrieve the message. --- src/binding.cc | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/binding.cc b/src/binding.cc index f2ceefc4..0dc722d9 100644 --- a/src/binding.cc +++ b/src/binding.cc @@ -129,6 +129,7 @@ public: { HandleScope scope; Connection *self = ObjectWrap::Unwrap(args.This()); + const char *lastErrorMessage; if(!args[0]->IsString()) { THROW("First parameter must be a string query"); } @@ -137,7 +138,8 @@ public: int result = self->Send(queryText); free(queryText); if(result == 0) { - THROW("PQsendQuery returned error code"); + lastErrorMessage = self->GetLastError(); + THROW(lastErrorMessage); } //TODO should we flush before throw? self->Flush(); @@ -615,6 +617,11 @@ private: { EmitError(PQerrorMessage(connection_)); } + + const char *GetLastError() + { + return PQerrorMessage(connection_); + } void StopWrite() { From c736ba14b3c751ecc06261eb2662af02a9589f0a Mon Sep 17 00:00:00 2001 From: Ben Montgomery Date: Thu, 26 Jul 2012 15:40:53 -0400 Subject: [PATCH 2/2] remove unnecessary whitespace --- src/binding.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/binding.cc b/src/binding.cc index 0dc722d9..e4a31bb7 100644 --- a/src/binding.cc +++ b/src/binding.cc @@ -138,8 +138,8 @@ public: int result = self->Send(queryText); free(queryText); if(result == 0) { - lastErrorMessage = self->GetLastError(); - THROW(lastErrorMessage); + lastErrorMessage = self->GetLastError(); + THROW(lastErrorMessage); } //TODO should we flush before throw? self->Flush(); @@ -617,7 +617,7 @@ private: { EmitError(PQerrorMessage(connection_)); } - + const char *GetLastError() { return PQerrorMessage(connection_);