chore: improve error handling

This commit is contained in:
DarshanBPatel 2024-10-08 15:59:03 +05:30
parent 13243f668e
commit 7a5cf6af9f
No known key found for this signature in database
GPG Key ID: 9A92CCD9899F0D22
2 changed files with 57 additions and 37 deletions

View File

@ -9,6 +9,11 @@
//This is a C-wrapper for the C++ library that helps in integrating negentropy with nim code. //This is a C-wrapper for the C++ library that helps in integrating negentropy with nim code.
//TODO: Do error handling by catching exceptions //TODO: Do error handling by catching exceptions
thread_local std::string lastError;
EXTERNC const char* get_last_error() {
return lastError.c_str();
}
void printHexString(std::string_view toPrint){ void printHexString(std::string_view toPrint){
for (size_t i = 0; i < toPrint.size(); ++i) { for (size_t i = 0; i < toPrint.size(); ++i) {
printf("%0hhx", toPrint[i]); printf("%0hhx", toPrint[i]);
@ -18,20 +23,27 @@ void printHexString(std::string_view toPrint){
void* storage_new(const char* db_path, const char* name){ void* storage_new(const char* db_path, const char* name){
negentropy::storage::BTreeMem* storage; negentropy::storage::BTreeMem* storage;
/* try {
auto env = lmdb::env::create(); /*
env.set_max_dbs(64); auto env = lmdb::env::create();
env.open(db_path, 0); env.set_max_dbs(64);
env.open(db_path, 0);
lmdb::dbi btreeDbi; lmdb::dbi btreeDbi;
{ {
auto txn = lmdb::txn::begin(env); auto txn = lmdb::txn::begin(env);
btreeDbi = negentropy::storage::BTreeMem::setupDB(txn, name); btreeDbi = negentropy::storage::BTreeMem::setupDB(txn, name);
txn.commit(); txn.commit();
} */ }
*/
storage = new negentropy::storage::BTreeMem();
} catch (const std::exception& e) {
lastError = e.what();
return nullptr;
}
storage = new negentropy::storage::BTreeMem();
return storage; return storage;
} }
@ -60,8 +72,8 @@ void* negentropy_new(void* storage, uint64_t frameSizeLimit){
try{ try{
ne = new Negentropy<negentropy::storage::BTreeMem>(*lmdbStorage, frameSizeLimit); ne = new Negentropy<negentropy::storage::BTreeMem>(*lmdbStorage, frameSizeLimit);
}catch(negentropy::err e){ }catch(negentropy::err e){
//TODO: Error handling lastError = e.what();
return NULL; return nullptr;
} }
return ne; return ne;
} }
@ -78,7 +90,7 @@ int negentropy_initiate(void* negentropy, result* result){
printHexString(std::string_view(*output)); */ printHexString(std::string_view(*output)); */
} catch(negentropy::err e){ } catch(negentropy::err e){
//std::cout << "Exception raised in initiate " << e.what() << std::endl; //std::cout << "Exception raised in initiate " << e.what() << std::endl;
//TODO: Error handling lastError = e.what();
return -1; return -1;
} }
if (output.size() > 0 ){ if (output.size() > 0 ){
@ -108,9 +120,12 @@ bool storage_insert(void* storage, uint64_t createdAt, buffer* id){
/* std::cout << "inserting entry in storage, createdAt:" << createdAt << ",id:"; /* std::cout << "inserting entry in storage, createdAt:" << createdAt << ",id:";
printHexString(data); */ printHexString(data); */
//TODO: Error handling. Is it required? try {
//How does out of memory get handled? return lmdbStorage->insert(createdAt, data);
return lmdbStorage->insert(createdAt, data); } catch (const std::exception& e) {
lastError = e.what();
return false;
}
} }
bool storage_erase(void* storage, uint64_t createdAt, buffer* id){ bool storage_erase(void* storage, uint64_t createdAt, buffer* id){
@ -121,8 +136,12 @@ bool storage_erase(void* storage, uint64_t createdAt, buffer* id){
/* std::cout << "erasing entry from storage, createdAt:" << createdAt << ",id:"; /* std::cout << "erasing entry from storage, createdAt:" << createdAt << ",id:";
printHexString(data); */ printHexString(data); */
//TODO: Error handling try {
return lmdbStorage->erase(createdAt, data); return lmdbStorage->erase(createdAt, data);
} catch (const std::exception& e) {
lastError = e.what();
return false;
}
} }
int reconcile(void* negentropy, buffer* query, result* result){ int reconcile(void* negentropy, buffer* query, result* result){
@ -136,7 +155,7 @@ int reconcile(void* negentropy, buffer* query, result* result){
} catch(negentropy::err e){ } catch(negentropy::err e){
//All errors returned are non-recoverable errors. //All errors returned are non-recoverable errors.
//So passing on the error message upwards //So passing on the error message upwards
//std::cout << "Exception raised in reconcile " << e.what() << std::endl; lastError = e.what();
result->error = (char*)calloc(strlen(e.what()), sizeof(char)); result->error = (char*)calloc(strlen(e.what()), sizeof(char));
strcpy(result->error,e.what()); strcpy(result->error,e.what());
return -1; return -1;
@ -239,11 +258,11 @@ int reconcile_with_ids_no_cbk(void* negentropy, buffer* query, result* result){
} catch(negentropy::err e){ } catch(negentropy::err e){
std::cout << "caught error "<< e.what() << std::endl; lastError = e.what();
result->error = (char*)calloc(strlen(e.what()), sizeof(char)); result->error = strdup(e.what());
strcpy(result->error,e.what());
return -1; return -1;
} }
buffer output = {0,NULL}; buffer output = {0,NULL};
if (out) { if (out) {
result->output.len = out.value().size(); result->output.len = out.value().size();
@ -293,7 +312,7 @@ void* subrange_new(void* storage, uint64_t startTimeStamp, uint64_t endTimeStamp
try { try {
subRange = new negentropy::storage::SubRange(*st, negentropy::Bound(startTimeStamp), negentropy::Bound(endTimeStamp)); subRange = new negentropy::storage::SubRange(*st, negentropy::Bound(startTimeStamp), negentropy::Bound(endTimeStamp));
} catch (negentropy::err e){ } catch (negentropy::err e){
//TODO: Error handling lastError = e.what();
return NULL; return NULL;
} }
return subRange; return subRange;
@ -324,7 +343,7 @@ void* negentropy_subrange_new(void* subrange, uint64_t frameSizeLimit){
try{ try{
ne = new Negentropy<negentropy::storage::SubRange>(*sub_range, frameSizeLimit); ne = new Negentropy<negentropy::storage::SubRange>(*sub_range, frameSizeLimit);
}catch(negentropy::err e){ }catch(negentropy::err e){
//TODO: Error handling lastError = e.what();
return NULL; return NULL;
} }
return ne; return ne;
@ -341,7 +360,7 @@ int negentropy_subrange_initiate(void* negentropy, result* result){
/* std::cout << "output of initiate is, len:" << output->size() << ", output:"; /* std::cout << "output of initiate is, len:" << output->size() << ", output:";
printHexString(std::string_view(*output)); */ printHexString(std::string_view(*output)); */
} catch(negentropy::err e){ } catch(negentropy::err e){
//std::cout << "Exception raised in initiate " << e.what() << std::endl; lastError = e.what();
return -1; return -1;
} }
if (output.size() > 0 ){ if (output.size() > 0 ){
@ -374,7 +393,7 @@ int reconcile_subrange(void* negentropy, buffer* query, result* result){
} catch(negentropy::err e){ } catch(negentropy::err e){
//All errors returned are non-recoverable errors. //All errors returned are non-recoverable errors.
//So passing on the error message upwards //So passing on the error message upwards
//std::cout << "Exception raised in reconcile " << e.what() << std::endl; lastError = e.what();
result->error = (char*)calloc(strlen(e.what()), sizeof(char)); result->error = (char*)calloc(strlen(e.what()), sizeof(char));
strcpy(result->error,e.what()); strcpy(result->error,e.what());
return -1; return -1;
@ -414,7 +433,7 @@ int reconcile_with_ids_subrange_no_cbk(void* negentropy, buffer* query, result*
} catch(negentropy::err e){ } catch(negentropy::err e){
std::cout << "caught error "<< e.what() << std::endl; lastError = e.what();
result->error = (char*)calloc(strlen(e.what()), sizeof(char)); result->error = (char*)calloc(strlen(e.what()), sizeof(char));
strcpy(result->error,e.what()); strcpy(result->error,e.what());
return -1; return -1;
@ -433,4 +452,3 @@ int reconcile_with_ids_subrange_no_cbk(void* negentropy, buffer* query, result*
} }
return 0; return 0;
} }

View File

@ -25,6 +25,8 @@ typedef struct _result_ {
//This is a C-wrapper for the C++ library that helps in integrating negentropy with nim code. //This is a C-wrapper for the C++ library that helps in integrating negentropy with nim code.
//TODO: Do error handling by catching exceptions //TODO: Do error handling by catching exceptions
EXTERNC const char* get_last_error();
EXTERNC void* storage_new(const char* db_path, const char* name); EXTERNC void* storage_new(const char* db_path, const char* name);
EXTERNC void storage_delete(void* storage); EXTERNC void storage_delete(void* storage);