This is an automated email from the ASF dual-hosted git repository.
lihaopeng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git
The following commit(s) were added to refs/heads/master by this push:
new 4cc4944ca13 [Refactor](common) refactor the Exception code (#38172)
4cc4944ca13 is described below
commit 4cc4944ca139948dab78459337e91b3612f2b8c5
Author: HappenLee <[email protected]>
AuthorDate: Mon Jul 22 14:11:15 2024 +0800
[Refactor](common) refactor the Exception code (#38172)
1. Remove the useless code in Exception
2. Use fmt replace the streamstring for performance
---
be/src/common/exception.cpp | 19 -------------------
be/src/common/exception.h | 18 +++++-------------
be/test/common/exception_test.cpp | 6 ------
3 files changed, 5 insertions(+), 38 deletions(-)
diff --git a/be/src/common/exception.cpp b/be/src/common/exception.cpp
index c6139c0f995..48e1229d44e 100644
--- a/be/src/common/exception.cpp
+++ b/be/src/common/exception.cpp
@@ -32,23 +32,4 @@ Exception::Exception(int code, const std::string_view& msg) {
LOG(FATAL) << "[ExitOnException] error code: " << code << ", message:
" << msg;
}
}
-
-Exception::Exception(const Exception& nested, int code, const
std::string_view& msg) {
- _code = code;
- _err_msg = std::make_unique<ErrMsg>();
- _err_msg->_msg = msg;
- if (ErrorCode::error_states[abs(code)].stacktrace) {
- _err_msg->_stack = get_stack_trace();
- }
- _nested_excption = std::make_unique<Exception>();
- _nested_excption->_code = nested._code;
- _nested_excption->_err_msg = std::make_unique<ErrMsg>();
- _nested_excption->_err_msg->_msg = nested._err_msg->_msg;
- _nested_excption->_err_msg->_stack = nested._err_msg->_stack;
-
- if (config::exit_on_exception) {
- LOG(FATAL) << "[ExitOnException] error code: " << code << ", message:
" << msg;
- }
-}
-
} // namespace doris
\ No newline at end of file
diff --git a/be/src/common/exception.h b/be/src/common/exception.h
index ce44e658749..b35ef7e8ff8 100644
--- a/be/src/common/exception.h
+++ b/be/src/common/exception.h
@@ -19,8 +19,8 @@
#include <fmt/format.h>
#include <gen_cpp/Status_types.h>
-#include <stdint.h>
+#include <cstdint>
#include <exception>
#include <memory>
#include <ostream>
@@ -39,9 +39,6 @@ public:
Exception() : _code(ErrorCode::OK) {}
Exception(int code, const std::string_view& msg);
Exception(const Status& status) : Exception(status.code(), status.msg()) {}
- // add nested exception as first param, or the template may could not find
- // the correct method for ...args
- Exception(const Exception& nested, int code, const std::string_view& msg);
// Format message with fmt::format, like the logging functions.
template <typename... Args>
@@ -63,7 +60,6 @@ private:
std::string _stack;
};
std::unique_ptr<ErrMsg> _err_msg;
- std::unique_ptr<Exception> _nested_excption;
mutable std::string _cache_string;
};
@@ -71,16 +67,12 @@ inline const std::string& Exception::to_string() const {
if (!_cache_string.empty()) {
return _cache_string;
}
- std::stringstream ostr;
- ostr << "[E" << _code << "] ";
- ostr << (_err_msg ? _err_msg->_msg : "");
+ fmt::memory_buffer buf;
+ fmt::format_to(buf, "[E{}] {}", _code, _err_msg ? _err_msg->_msg : "");
if (_err_msg && !_err_msg->_stack.empty()) {
- ostr << '\n' << _err_msg->_stack;
+ fmt::format_to(buf, "\n{}", _err_msg->_stack);
}
- if (_nested_excption != nullptr) {
- ostr << '\n' << "Caused by:" << _nested_excption->to_string();
- }
- _cache_string = ostr.str();
+ _cache_string = fmt::to_string(buf);
return _cache_string;
}
diff --git a/be/test/common/exception_test.cpp
b/be/test/common/exception_test.cpp
index 344c0bb1faf..0878c394281 100644
--- a/be/test/common/exception_test.cpp
+++ b/be/test/common/exception_test.cpp
@@ -50,12 +50,6 @@ TEST_F(ExceptionTest, NestedError) {
throw doris::Exception(ErrorCode::OS_ERROR, "test OS_ERROR {}", "bug");
} catch (doris::Exception& e1) {
EXPECT_TRUE(e1.to_string().find("OS_ERROR") != std::string::npos);
- try {
- throw doris::Exception(e1, ErrorCode::INVALID_ARGUMENT, "test
INVALID_ARGUMENT");
- } catch (doris::Exception& e2) {
- EXPECT_TRUE(e2.to_string().find("OS_ERROR") != std::string::npos);
- EXPECT_TRUE(e2.to_string().find("INVALID_ARGUMENT") !=
std::string::npos);
- }
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]