From a9635a3d8d5726d8e632da2a9c8988d00b44f44d Mon Sep 17 00:00:00 2001 From: Ben Montgomery Date: Tue, 24 Jul 2012 12:53:00 -0400 Subject: [PATCH] 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() {