From 3399352383afa48866053853712a47fddd201809 Mon Sep 17 00:00:00 2001 From: brianc Date: Fri, 4 Mar 2011 22:28:40 +0000 Subject: [PATCH] errors from libpq come back with appropriate data --- src/binding.cc | 60 +++++++++++++++++-- .../client/error-handling-tests.js | 2 - 2 files changed, 56 insertions(+), 6 deletions(-) diff --git a/src/binding.cc b/src/binding.cc index c5aad018..35b0494b 100644 --- a/src/binding.cc +++ b/src/binding.cc @@ -19,6 +19,18 @@ static Persistent error_symbol; static Persistent ready_symbol; static Persistent row_symbol; static Persistent notice_symbol; +static Persistent severity_symbol; +static Persistent code_symbol; +static Persistent message_symbol; +static Persistent detail_symbol; +static Persistent hint_symbol; +static Persistent position_symbol; +static Persistent internalPosition_symbol; +static Persistent internalQuery_symbol; +static Persistent where_symbol; +static Persistent file_symbol; +static Persistent line_symbol; +static Persistent routine_symbol; class Connection : public EventEmitter { @@ -40,6 +52,18 @@ public: ready_symbol = NODE_PSYMBOL("_readyForQuery"); notice_symbol = NODE_PSYMBOL("notice"); row_symbol = NODE_PSYMBOL("_row"); + severity_symbol = NODE_PSYMBOL("severity"); + code_symbol = NODE_PSYMBOL("code"); + message_symbol = NODE_PSYMBOL("message"); + detail_symbol = NODE_PSYMBOL("detail"); + hint_symbol = NODE_PSYMBOL("hint"); + position_symbol = NODE_PSYMBOL("position"); + internalPosition_symbol = NODE_PSYMBOL("internalPosition"); + internalQuery_symbol = NODE_PSYMBOL("internalQuery"); + where_symbol = NODE_PSYMBOL("where"); + file_symbol = NODE_PSYMBOL("file"); + line_symbol = NODE_PSYMBOL("line"); + routine_symbol = NODE_PSYMBOL("routine"); NODE_SET_PROTOTYPE_METHOD(t, "connect", Connect); NODE_SET_PROTOTYPE_METHOD(t, "_sendQuery", SendQuery); @@ -329,7 +353,7 @@ protected: } } - void HandleResult(PGresult* result) + void HandleResult(const PGresult* result) { ExecStatusType status = PQresultStatus(result); switch(status) { @@ -337,7 +361,7 @@ protected: HandleTuplesResult(result); break; case PGRES_FATAL_ERROR: - EmitLastError(); + HandleErrorResult(result); break; case PGRES_COMMAND_OK: case PGRES_EMPTY_QUERY: @@ -349,7 +373,7 @@ protected: } } - void HandleTuplesResult(PGresult* result) + void HandleTuplesResult(const PGresult* result) { int rowCount = PQntuples(result); for(int rowNumber = 0; rowNumber < rowCount; rowNumber++) { @@ -374,7 +398,7 @@ protected: } } - Handle WrapFieldValue(PGresult* result, int rowNumber, int fieldNumber) + Handle WrapFieldValue(const PGresult* result, int rowNumber, int fieldNumber) { int fieldType = PQftype(result, fieldNumber); char* fieldValue = PQgetvalue(result, rowNumber, fieldNumber); @@ -386,6 +410,34 @@ protected: } } + void HandleErrorResult(const PGresult* result) + { + HandleScope scope; + Local msg = Object::New(); + AttachErrorField(result, msg, severity_symbol, PG_DIAG_SEVERITY); + AttachErrorField(result, msg, code_symbol, PG_DIAG_SQLSTATE); + AttachErrorField(result, msg, message_symbol, PG_DIAG_MESSAGE_PRIMARY); + AttachErrorField(result, msg, detail_symbol, PG_DIAG_MESSAGE_DETAIL); + AttachErrorField(result, msg, hint_symbol, PG_DIAG_MESSAGE_HINT); + AttachErrorField(result, msg, position_symbol, PG_DIAG_STATEMENT_POSITION); + AttachErrorField(result, msg, internalPosition_symbol, PG_DIAG_INTERNAL_POSITION); + AttachErrorField(result, msg, internalQuery_symbol, PG_DIAG_INTERNAL_QUERY); + AttachErrorField(result, msg, where_symbol, PG_DIAG_CONTEXT); + AttachErrorField(result, msg, file_symbol, PG_DIAG_SOURCE_FILE); + AttachErrorField(result, msg, line_symbol, PG_DIAG_SOURCE_LINE); + AttachErrorField(result, msg, routine_symbol, PG_DIAG_SOURCE_FUNCTION); + Handle m = msg; + Emit(error_symbol, 1, &m); + } + + void AttachErrorField(const PGresult *result, const Local msg, const Persistent symbol, int fieldcode) + { + char *val = PQresultErrorField(result, fieldcode); + if(val) { + msg->Set(symbol, String::New(val)); + } + } + void End() { StopRead(); diff --git a/test/integration/client/error-handling-tests.js b/test/integration/client/error-handling-tests.js index 0533070a..b5871c82 100644 --- a/test/integration/client/error-handling-tests.js +++ b/test/integration/client/error-handling-tests.js @@ -19,7 +19,6 @@ test('error handling', function(){ assert.emits(query, 'error', function(error) { test('error is a psql error', function() { - return false; assert.equal(error.severity, "ERROR"); }) }); @@ -64,7 +63,6 @@ test('error handling', function(){ test("query emits the error", function() { assert.emits(query, 'error', function(err) { test('error has right severity', function() { - return false; assert.equal(err.severity, "ERROR"); })