From 95a6cbf8e082db7cbd14dcc623fda221084eb7f9 Mon Sep 17 00:00:00 2001 From: kailixu Date: Wed, 12 Jun 2024 18:56:29 +0800 Subject: [PATCH] enh: support get origin string of taos errors --- include/util/taoserror.h | 7 +++++++ source/util/src/terror.c | 21 ++++++++++----------- source/util/test/CMakeLists.txt | 10 +++++++++- source/util/test/terrorTest.cpp | 29 +++++++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 12 deletions(-) create mode 100644 source/util/test/terrorTest.cpp diff --git a/include/util/taoserror.h b/include/util/taoserror.h index 8f8434dfc1..63b733a337 100644 --- a/include/util/taoserror.h +++ b/include/util/taoserror.h @@ -24,6 +24,12 @@ extern "C" { // clang-format off +typedef struct { + int32_t val; + const char* str; + const char* origin; +} STaosError; + #define TAOS_DEF_ERROR_CODE(mod, code) ((int32_t)((0x80000000 | ((mod)<<16) | (code)))) #define TAOS_SYSTEM_ERROR(code) (0x80ff0000 | (code)) @@ -38,6 +44,7 @@ const char* terrstr(); char* taosGetErrMsgReturn(); char* taosGetErrMsg(); int32_t* taosGetErrno(); +int32_t taosGetErrSize(); #define terrno (*taosGetErrno()) #define terrMsg (taosGetErrMsg()) diff --git a/source/util/src/terror.c b/source/util/src/terror.c index 0f594af0e9..f2ae43bc94 100644 --- a/source/util/src/terror.c +++ b/source/util/src/terror.c @@ -21,10 +21,10 @@ #define TAOS_ERROR_C -typedef struct { - int32_t val; - const char* str; -} STaosError; +// typedef struct { +// int32_t val; +// const char* str; +// } STaosError; static threadlocal int32_t tsErrno; static threadlocal char tsErrMsgDetail[ERR_MSG_LEN] = {0}; @@ -35,7 +35,9 @@ char* taosGetErrMsg() { return tsErrMsgDetail; } char* taosGetErrMsgReturn() { return tsErrMsgReturn; } #ifdef TAOS_ERROR_C -#define TAOS_DEFINE_ERROR(name, msg) {.val = (name), .str = (msg)}, +#define TAOS_DEFINE_ERROR(name, msg) {.val = (name), .str = (msg), .origin = #name}, +STaosError errors[] = { + TAOS_DEFINE_ERROR(TSDB_CODE_SUCCESS, "success") #else #define TAOS_DEFINE_ERROR(name, mod, code, msg) static const int32_t name = TAOS_DEF_ERROR_CODE(mod, code); #endif @@ -44,11 +46,6 @@ char* taosGetErrMsgReturn() { return tsErrMsgReturn; } #define TAOS_SUCCEEDED(err) ((err) >= 0) #define TAOS_FAILED(err) ((err) < 0) -#ifdef TAOS_ERROR_C -STaosError errors[] = { - {.val = 0, .str = "success"}, -#endif - // rpc TAOS_DEFINE_ERROR(TSDB_CODE_RPC_NETWORK_UNAVAIL, "Unable to establish connection") TAOS_DEFINE_ERROR(TSDB_CODE_RPC_FQDN_ERROR, "Unable to resolve FQDN") @@ -784,7 +781,7 @@ TAOS_DEFINE_ERROR(TSDB_CODE_TDLITE_IVLD_OPEN_DIR, "Invalid TDLite open TAOS_DEFINE_ERROR(TSDB_CODE_UTIL_QUEUE_OUT_OF_MEMORY, "Queue out of memory") -#ifdef TAOS_ERROR_C +#if defined(TAOS_ERROR_INFO) || defined(TAOS_ERROR_C) }; #endif @@ -837,3 +834,5 @@ const char* tstrerror(int32_t err) { } const char* terrstr() { return tstrerror(terrno); } + +int32_t taosGetErrSize() { return sizeof(errors)/sizeof(errors[0]); } diff --git a/source/util/test/CMakeLists.txt b/source/util/test/CMakeLists.txt index e8e3348343..89978fd5aa 100644 --- a/source/util/test/CMakeLists.txt +++ b/source/util/test/CMakeLists.txt @@ -123,4 +123,12 @@ add_test( #add_test( # NAME decompressTest # COMMAND decompressTest -#) \ No newline at end of file +#) + +# terrorTest +add_executable(terrorTest "terrorTest.cpp") +target_link_libraries(terrorTest os util common gtest_main) +add_test( + NAME terrorTest + COMMAND terrorTest +) \ No newline at end of file diff --git a/source/util/test/terrorTest.cpp b/source/util/test/terrorTest.cpp new file mode 100644 index 0000000000..db2f9641ed --- /dev/null +++ b/source/util/test/terrorTest.cpp @@ -0,0 +1,29 @@ +#include +#include + +#define TAOS_ERROR_INFO + +#include +#include "os.h" +#include "osTime.h" +#include "taos.h" +#include "taoserror.h" +#include "tglobal.h" + +extern STaosError errors[]; + +using namespace std; + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wwrite-strings" +#pragma GCC diagnostic ignored "-Wunused-function" +#pragma GCC diagnostic ignored "-Wunused-variable" +#pragma GCC diagnostic ignored "-Wsign-compare" + +TEST(TAOS_ERROR_TEST, terror_test) { + int32_t errSize = taosGetErrSize(); + for (int32_t i = 0; i < errSize; ++i) { + STaosError *pInfo = &errors[i]; + std::cout << i + 1 << " " << pInfo->origin << " " << pInfo->val << std::endl; + } +} \ No newline at end of file