Skip to content
This repository has been archived by the owner on Dec 1, 2024. It is now read-only.

Commit

Permalink
Fix error messages (needs to be copied)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lars-Magnus Skog committed Dec 15, 2018
1 parent 57b0be4 commit 5613fb5
Showing 1 changed file with 29 additions and 6 deletions.
35 changes: 29 additions & 6 deletions napi/leveldown.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct BaseWorker {
Database* database,
napi_value callback,
const char* resourceName)
: env_(env), database_(database) {
: env_(env), database_(database), errMsg_(NULL) {
napi_create_reference(env_, callback, 1, &callbackRef_);
napi_value asyncResourceName;
napi_create_string_utf8(env_, resourceName,
Expand All @@ -35,6 +35,7 @@ struct BaseWorker {
}

virtual ~BaseWorker () {
delete [] errMsg_;
napi_delete_reference(env_, callbackRef_);
napi_delete_async_work(env_, asyncWork_);
}
Expand All @@ -47,6 +48,26 @@ struct BaseWorker {
self->DoExecute();
}

/**
* Store status. Note that error message has to be copied.
*/
void SetStatus (leveldb::Status status) {
status_ = status;
if (!status.ok()) {
SetErrorMessage(status.ToString().c_str());
}
}

/**
* Copied error message.
*/
void SetErrorMessage(const char *msg) {
delete [] errMsg_;
size_t size = strlen(msg) + 1;
errMsg_ = new char[size];
memcpy(errMsg_, msg, size);
}

/**
* MUST be overriden.
*/
Expand Down Expand Up @@ -79,9 +100,8 @@ struct BaseWorker {
napi_value callback;
napi_get_reference_value(env_, callbackRef_, &callback);

const char* str = status_.ToString().c_str();
napi_value msg;
napi_create_string_utf8(env_, str, strlen(str), &msg);
napi_create_string_utf8(env_, errMsg_, strlen(errMsg_), &msg);

const int argc = 1;
napi_value argv[argc];
Expand Down Expand Up @@ -114,7 +134,10 @@ struct BaseWorker {
napi_ref callbackRef_;
napi_async_work asyncWork_;
Database* database_;

private:
leveldb::Status status_;
char *errMsg_;
};

/**
Expand Down Expand Up @@ -351,7 +374,7 @@ struct OpenWorker : public BaseWorker {
}

virtual void DoExecute () {
status_ = database_->Open(options_, location_);
SetStatus(database_->Open(options_, location_));
}

leveldb::Options options_;
Expand Down Expand Up @@ -500,7 +523,7 @@ struct PutWorker : public BaseWorker {
}

virtual void DoExecute () {
status_ = database_->Put(options_, key_, value_);
SetStatus(database_->Put(options_, key_, value_));
}

leveldb::WriteOptions options_;
Expand Down Expand Up @@ -1057,7 +1080,7 @@ struct NextWorker : public BaseWorker {
virtual void DoExecute () {
ok_ = iterator_->IteratorNext(result_);
if (!ok_) {
status_ = iterator_->IteratorStatus();
SetStatus(iterator_->IteratorStatus());
}
}

Expand Down

0 comments on commit 5613fb5

Please sign in to comment.