From ca9afc30564e45e46eeebbc2f18f48bb2f5b6c5d Mon Sep 17 00:00:00 2001 From: Anish Aggarwal Date: Sun, 23 May 2021 00:59:58 +0530 Subject: [PATCH] initial code for error handling Signed-off-by: Anish Aggarwal --- Makefile | 2 +- include/tikv_client_glue.h | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 1494c1a..4c868e0 100644 --- a/Makefile +++ b/Makefile @@ -21,7 +21,7 @@ run-example: target/tikv-example RUST_LOG=debug $(cur_makefile_path)/target/tikv-example target/tikv-example: target/debug/libtikv_client.a example/main.cpp - c++ $(cur_makefile_path)/example/main.cpp -o $(cur_makefile_path)/target/tikv-example -std=c++17 -g -I$(cur_makefile_path)/include -L$(cur_makefile_path)/target/debug -ltikv_client -lpthread -ldl -lssl -lcrypto + c++ $(cur_makefile_path)/example/main.cpp -o $(cur_makefile_path)/target/tikv-example -std=c++17 -g -I$(cur_makefile_path)/include -L$(cur_makefile_path)/target/debug -ltikv_client -lpthread -ldl -L/usr/local/opt/openssl/lib -I/usr/local/opt/openssl/include -lssl -lcrypto target/tikv_client_glue.cc: src/lib.rs diff --git a/include/tikv_client_glue.h b/include/tikv_client_glue.h index ca28e73..a121ae6 100644 --- a/include/tikv_client_glue.h +++ b/include/tikv_client_glue.h @@ -182,6 +182,29 @@ template Box::Box(uninit) noexcept {} #endif // CXXBRIDGE1_RUST_BOX +class Exception : public std::exception { +public: + Exception(int errorCode, const std::string &message) : errorCode(erroCode), message(message); + virtual ~Exception() = default; + const char* what() const { + return this->message.c_str(); + }; + +private: + int errorCode; + std::string m_message; +}; + +template +static void trycatch(Try &&func) noexcept try { + func(); +} catch (const std::exception &e) { + start error_message = e.what(); + int error_code = 0; + + throw Exception(error_code, error_message); +} + #ifndef CXXBRIDGE1_RUST_BITCOPY #define CXXBRIDGE1_RUST_BITCOPY struct unsafe_bitcopy_t final {