Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

*: improve RUNTIME_CHECK and RUNTIME_ASSERT #5434

Merged
merged 13 commits into from
Jul 27, 2022
52 changes: 33 additions & 19 deletions dbms/src/Common/Exception.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include <errno.h>
#include <string.h>


namespace DB
{
namespace ErrorCodes
Expand All @@ -35,7 +34,6 @@ extern const int UNKNOWN_EXCEPTION;
extern const int CANNOT_TRUNCATE_FILE;
} // namespace ErrorCodes


void throwFromErrno(const std::string & s, int code, int e)
{
const size_t buf_size = 128;
Expand All @@ -54,14 +52,18 @@ void throwFromErrno(const std::string & s, int code, int e)
strcpy(buf, unknown_message);
strcpy(buf + strlen(unknown_message), code);
}
throw ErrnoException(s + ", errno: " + toString(e) + ", strerror: " + std::string(buf), code, e);
throw ErrnoException(s + ", errno: " + toString(e) + ", strerror: " + std::string(buf),
code,
e);
#else
throw ErrnoException(s + ", errno: " + toString(e) + ", strerror: " + std::string(strerror_r(e, buf, sizeof(buf))), code, e);
throw ErrnoException(s + ", errno: " + toString(e) + ", strerror: " + std::string(strerror_r(e, buf, sizeof(buf))),
code,
e);
#endif
}


void tryLogCurrentException(const char * log_name, const std::string & start_of_message)
void tryLogCurrentException(const char * log_name,
const std::string & start_of_message)
{
tryLogCurrentException(&Poco::Logger::get(log_name), start_of_message);
}
Expand All @@ -75,19 +77,22 @@ void tryLogCurrentException(const char * log_name, const std::string & start_of_
{ \
}

void tryLogCurrentException(const LoggerPtr & logger, const std::string & start_of_message)
void tryLogCurrentException(const LoggerPtr & logger,
const std::string & start_of_message)
{
TRY_LOG_CURRENT_EXCEPTION(logger, start_of_message);
}

void tryLogCurrentException(Poco::Logger * logger, const std::string & start_of_message)
void tryLogCurrentException(Poco::Logger * logger,
const std::string & start_of_message)
{
TRY_LOG_CURRENT_EXCEPTION(logger, start_of_message);
}

#undef TRY_LOG_CURRENT_EXCEPTION

std::string getCurrentExceptionMessage(bool with_stacktrace, bool check_embedded_stacktrace)
std::string getCurrentExceptionMessage(bool with_stacktrace,
bool check_embedded_stacktrace)
{
std::stringstream stream;

Expand All @@ -103,8 +108,10 @@ std::string getCurrentExceptionMessage(bool with_stacktrace, bool check_embedded
{
try
{
stream << "Poco::Exception. Code: " << ErrorCodes::POCO_EXCEPTION << ", e.code() = " << e.code()
<< ", e.displayText() = " << e.displayText() << ", e.what() = " << e.what();
stream << "Poco::Exception. Code: " << ErrorCodes::POCO_EXCEPTION
<< ", e.code() = " << e.code()
<< ", e.displayText() = " << e.displayText()
<< ", e.what() = " << e.what();
}
catch (...)
{
Expand All @@ -120,7 +127,8 @@ std::string getCurrentExceptionMessage(bool with_stacktrace, bool check_embedded
if (status)
name += " (demangling status: " + toString(status) + ")";

stream << "std::exception. Code: " << ErrorCodes::STD_EXCEPTION << ", type: " << name << ", e.what() = " << e.what();
stream << "std::exception. Code: " << ErrorCodes::STD_EXCEPTION
<< ", type: " << name << ", e.what() = " << e.what();
}
catch (...)
{
Expand All @@ -136,7 +144,8 @@ std::string getCurrentExceptionMessage(bool with_stacktrace, bool check_embedded
if (status)
name += " (demangling status: " + toString(status) + ")";

stream << "Unknown exception. Code: " << ErrorCodes::UNKNOWN_EXCEPTION << ", type: " << name;
stream << "Unknown exception. Code: " << ErrorCodes::UNKNOWN_EXCEPTION
<< ", type: " << name;
}
catch (...)
{
Expand All @@ -146,7 +155,6 @@ std::string getCurrentExceptionMessage(bool with_stacktrace, bool check_embedded
return stream.str();
}


int getCurrentExceptionCode()
{
try
Expand All @@ -171,15 +179,13 @@ int getCurrentExceptionCode()
}
}


void rethrowFirstException(const Exceptions & exceptions)
{
for (const auto & exception : exceptions)
if (exception)
std::rethrow_exception(exception);
}


std::string getExceptionMessage(const Exception & e, bool with_stacktrace, bool check_embedded_stacktrace)
{
std::stringstream stream;
Expand All @@ -200,7 +206,8 @@ std::string getExceptionMessage(const Exception & e, bool with_stacktrace, bool
}
}

stream << "Code: " << e.code() << ", e.displayText() = " << text << ", e.what() = " << e.what();
stream << "Code: " << e.code() << ", e.displayText() = " << text
<< ", e.what() = " << e.what();

if (with_stacktrace && !has_embedded_stack_trace)
stream << ", Stack trace:\n\n"
Expand All @@ -225,7 +232,6 @@ std::string getExceptionMessage(std::exception_ptr e, bool with_stacktrace)
}
}


std::string ExecutionStatus::serializeText() const
{
WriteBufferFromOwnString wb;
Expand Down Expand Up @@ -254,11 +260,19 @@ bool ExecutionStatus::tryDeserializeText(const std::string & data)
return true;
}

ExecutionStatus ExecutionStatus::fromCurrentException(const std::string & start_of_message)
ExecutionStatus
ExecutionStatus::fromCurrentException(const std::string & start_of_message)
{
String msg = (start_of_message.empty() ? "" : (start_of_message + ": ")) + getCurrentExceptionMessage(false, true);
return ExecutionStatus(getCurrentExceptionCode(), msg);
}

namespace exception_details
{
Poco::Logger * getDefaultFatalLogger()
{
return &Poco::Logger::get("DefaultFatal");
}
} // namespace exception_details

} // namespace DB
Loading