Skip to content

Commit a9635a3

Browse files
benmontybrianc
authored andcommitted
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.
1 parent f5b49f1 commit a9635a3

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/binding.cc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ class Connection : public ObjectWrap {
129129
{
130130
HandleScope scope;
131131
Connection *self = ObjectWrap::Unwrap<Connection>(args.This());
132+
const char *lastErrorMessage;
132133
if(!args[0]->IsString()) {
133134
THROW("First parameter must be a string query");
134135
}
@@ -137,7 +138,8 @@ class Connection : public ObjectWrap {
137138
int result = self->Send(queryText);
138139
free(queryText);
139140
if(result == 0) {
140-
THROW("PQsendQuery returned error code");
141+
lastErrorMessage = self->GetLastError();
142+
THROW(lastErrorMessage);
141143
}
142144
//TODO should we flush before throw?
143145
self->Flush();
@@ -615,6 +617,11 @@ class Connection : public ObjectWrap {
615617
{
616618
EmitError(PQerrorMessage(connection_));
617619
}
620+
621+
const char *GetLastError()
622+
{
623+
return PQerrorMessage(connection_);
624+
}
618625

619626
void StopWrite()
620627
{

0 commit comments

Comments
 (0)