Skip to content

Commit

Permalink
[Refactor](common) refactor the Exception code (apache#38172)
Browse files Browse the repository at this point in the history
1. Remove the useless code in Exception
2. Use fmt replace the streamstring for performance
  • Loading branch information
HappenLee authored Jul 22, 2024
1 parent 1bf849c commit 4cc4944
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 38 deletions.
19 changes: 0 additions & 19 deletions be/src/common/exception.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 5 additions & 13 deletions be/src/common/exception.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -39,9 +39,6 @@ class Exception : public std::exception {
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>
Expand All @@ -63,24 +60,19 @@ class Exception : public std::exception {
std::string _stack;
};
std::unique_ptr<ErrMsg> _err_msg;
std::unique_ptr<Exception> _nested_excption;
mutable std::string _cache_string;
};

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;
}

Expand Down
6 changes: 0 additions & 6 deletions be/test/common/exception_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

Expand Down

0 comments on commit 4cc4944

Please sign in to comment.