Skip to content

Commit

Permalink
Improve error message accuracy of native SendQuery
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
benmonty authored and brianc committed Aug 7, 2012
1 parent f5b49f1 commit a9635a3
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ class Connection : public ObjectWrap {
{
HandleScope scope;
Connection *self = ObjectWrap::Unwrap<Connection>(args.This());
const char *lastErrorMessage;
if(!args[0]->IsString()) {
THROW("First parameter must be a string query");
}
Expand All @@ -137,7 +138,8 @@ class Connection : public ObjectWrap {
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();
Expand Down Expand Up @@ -615,6 +617,11 @@ class Connection : public ObjectWrap {
{
EmitError(PQerrorMessage(connection_));
}

const char *GetLastError()
{
return PQerrorMessage(connection_);
}

void StopWrite()
{
Expand Down

0 comments on commit a9635a3

Please sign in to comment.