Permalink
Browse files

Fixing issue where error message would be lost while handling errors …

…for multiple queries
  • Loading branch information...
1 parent cda0172 commit 8ed33ed76f711d592ae7e63df67e97f76059b83a @mariano committed Nov 10, 2011
Showing with 31 additions and 13 deletions.
  1. +16 −5 exception.cc
  2. +6 −3 exception.h
  3. +7 −3 query.cc
  4. +2 −2 query.h
View
@@ -1,14 +1,25 @@
// Copyright 2011 Mariano Iglesias <mgiglesias@gmail.com>
#include "./exception.h"
-node_db::Exception::Exception(const char* message) : exception(),
- message(message) {
+node_db::Exception::Exception(const char* message) throw() : exception() {
+ this->setMessage(message);
}
-node_db::Exception::Exception(const std::string& message) : exception(),
- message(message.c_str()) {
+node_db::Exception::Exception(const std::string& message) throw() : exception() {
+ this->setMessage(message.c_str());
+}
+
+node_db::Exception::~Exception() throw() {
+}
+
+void node_db::Exception::setMessage(const char* message) throw() {
+ this->message = message;
}
const char* node_db::Exception::what() const throw() {
- return this->message;
+ return (!this->message.empty() ? this->message.c_str() : NULL);
+}
+
+std::string::size_type node_db::Exception::size() throw() {
+ return (!this->message.empty() ? this->message.size() : 0);
}
View
@@ -8,11 +8,14 @@
namespace node_db {
class Exception : public std::exception {
public:
- explicit Exception(const char* message);
- explicit Exception(const std::string& message);
+ explicit Exception(const char* message) throw();
+ explicit Exception(const std::string& message) throw();
+ ~Exception() throw();
const char* what() const throw();
+ std::string::size_type size() throw();
+ void setMessage(const char* message) throw();
protected:
- const char* message;
+ std::string message;
};
}
View
@@ -724,7 +724,7 @@ node_db::Query::eioExecute(eio_req* eioRequest) {
} catch(const node_db::Exception& exception) {
request->query->connection->unlock();
Query::freeRequest(request, false);
- request->error = exception.what();
+ request->error = new std::string(exception.what());
}
#if !NODE_VERSION_AT_LEAST(0, 5, 0)
return 0;
@@ -795,7 +795,7 @@ int node_db::Query::eioExecuteFinished(eio_req* eioRequest) {
}
} else {
v8::Local<v8::Value> argv[1];
- argv[0] = v8::String::New(request->error != NULL ? request->error : "(unknown error)");
+ argv[0] = v8::String::New(request->error != NULL ? request->error->c_str() : "(unknown error)");
request->query->Emit("error", 1, argv);
@@ -904,7 +904,7 @@ void node_db::Query::executeAsync(execute_request_t* request) {
this->connection->unlock();
v8::Local<v8::Value> argv[1];
- argv[0] = v8::String::New(request->error != NULL ? exception.what() : "(unknown error)");
+ argv[0] = v8::String::New(exception.what());
this->Emit("error", 1, argv);
@@ -945,6 +945,10 @@ void node_db::Query::freeRequest(execute_request_t* request, bool freeAll) {
delete request->rows;
}
+ if (request->error != NULL) {
+ delete request->error;
+ }
+
if (freeAll) {
if (request->result != NULL) {
delete request->result;
View
@@ -33,8 +33,8 @@ class Query : public EventEmitter {
struct execute_request_t {
v8::Persistent<v8::Object> context;
Query* query;
- Result *result;
- const char* error;
+ Result* result;
+ std::string* error;
uint16_t columnCount;
bool buffered;
std::vector<row_t*>* rows;

0 comments on commit 8ed33ed

Please sign in to comment.