diff --git a/be/src/common/exception.cpp b/be/src/common/exception.cpp index 371a369b7be079..297b07222de364 100644 --- a/be/src/common/exception.cpp +++ b/be/src/common/exception.cpp @@ -48,6 +48,14 @@ Exception::Exception(int code, const std::string_view& msg, bool from_status) { // std::cout << "Exception: " << code << ", " << msg << ", " << get_stack_trace(0, "DISABLED") // << std::endl; #endif + + fmt::memory_buffer buf; + fmt::format_to(buf, "[E{}] {}", _code, _err_msg->_msg); + if (!_err_msg->_stack.empty()) { + fmt::format_to(buf, "\n{}", _err_msg->_stack); + } + _cache_string = fmt::to_string(buf); + if (config::exit_on_exception) { LOG(FATAL) << "[ExitOnException] error code: " << code << ", message: " << msg; } diff --git a/be/src/common/exception.h b/be/src/common/exception.h index 19c4c5d445aa7d..e03286c20e334c 100644 --- a/be/src/common/exception.h +++ b/be/src/common/exception.h @@ -46,9 +46,9 @@ class Exception : public std::exception { int code() const { return _code; } std::string message() const { return _err_msg ? _err_msg->_msg : ""; } - const std::string& to_string() const; + const std::string& to_string() const { return _cache_string; } - const char* what() const noexcept override { return to_string().c_str(); } + const char* what() const noexcept override { return _cache_string.c_str(); } Status to_status() const { return {code(), _err_msg->_msg, _err_msg->_stack}; } @@ -61,22 +61,8 @@ class Exception : public std::exception { std::string _stack; }; std::unique_ptr _err_msg; - mutable std::string _cache_string; + std::string _cache_string {}; }; - -inline const std::string& Exception::to_string() const { - if (!_cache_string.empty()) { - return _cache_string; - } - fmt::memory_buffer buf; - fmt::format_to(buf, "[E{}] {}", _code, _err_msg ? _err_msg->_msg : ""); - if (_err_msg && !_err_msg->_stack.empty()) { - fmt::format_to(buf, "\n{}", _err_msg->_stack); - } - _cache_string = fmt::to_string(buf); - return _cache_string; -} - } // namespace doris #define RETURN_IF_CATCH_EXCEPTION(stmt) \