2012-05-14 20:37:10 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
2014-07-21 09:11:20 +00:00
|
|
|
#include <cxxabi.h>
|
2012-05-14 20:37:10 +00:00
|
|
|
|
2015-09-29 19:19:54 +00:00
|
|
|
#include <common/logger_useful.h>
|
2013-11-18 19:18:03 +00:00
|
|
|
|
2013-06-21 20:34:19 +00:00
|
|
|
#include <DB/IO/WriteHelpers.h>
|
2012-05-14 20:37:10 +00:00
|
|
|
|
2010-03-01 16:59:51 +00:00
|
|
|
#include <DB/Core/Exception.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2011-12-12 06:15:34 +00:00
|
|
|
|
2012-11-15 11:59:39 +00:00
|
|
|
void throwFromErrno(const std::string & s, int code, int e)
|
2012-05-14 20:37:10 +00:00
|
|
|
{
|
|
|
|
char buf[128];
|
2014-11-17 00:41:40 +00:00
|
|
|
throw ErrnoException(s + ", errno: " + toString(e) + ", strerror: " + std::string(strerror_r(e, buf, sizeof(buf))), code, e);
|
2012-05-14 20:37:10 +00:00
|
|
|
}
|
|
|
|
|
2013-11-02 23:42:10 +00:00
|
|
|
|
|
|
|
ExceptionPtr cloneCurrentException()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
catch (const Exception & e)
|
|
|
|
{
|
|
|
|
return e.clone();
|
|
|
|
}
|
|
|
|
catch (const Poco::Exception & e)
|
|
|
|
{
|
|
|
|
return e.clone();
|
|
|
|
}
|
|
|
|
catch (const std::exception & e)
|
|
|
|
{
|
|
|
|
return new Exception(e.what(), ErrorCodes::STD_EXCEPTION);
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
return new Exception("Unknown exception", ErrorCodes::UNKNOWN_EXCEPTION);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-06 16:47:35 +00:00
|
|
|
inline std::string demangle(const char * const mangled, int & status)
|
|
|
|
{
|
|
|
|
const auto demangled_str = abi::__cxa_demangle(mangled, 0, 0, &status);
|
|
|
|
std::string demangled{demangled_str};
|
|
|
|
free(demangled_str);
|
|
|
|
|
|
|
|
return demangled;
|
|
|
|
}
|
2013-11-02 23:42:10 +00:00
|
|
|
|
2015-08-12 03:57:32 +00:00
|
|
|
void tryLogCurrentException(const char * log_name, const std::string & start_of_message)
|
2015-03-27 13:10:35 +00:00
|
|
|
{
|
2015-08-12 03:57:32 +00:00
|
|
|
tryLogCurrentException(&Logger::get(log_name), start_of_message);
|
2015-03-27 13:10:35 +00:00
|
|
|
}
|
|
|
|
|
2015-08-12 03:57:32 +00:00
|
|
|
void tryLogCurrentException(Poco::Logger * logger, const std::string & start_of_message)
|
2013-11-18 19:18:03 +00:00
|
|
|
{
|
2015-06-29 21:35:35 +00:00
|
|
|
try
|
|
|
|
{
|
2015-08-12 03:57:32 +00:00
|
|
|
LOG_ERROR(logger, start_of_message << (start_of_message.empty() ? "" : ": ") << getCurrentExceptionMessage(true));
|
2015-06-29 21:35:35 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string getCurrentExceptionMessage(bool with_stacktrace)
|
|
|
|
{
|
|
|
|
std::stringstream stream;
|
|
|
|
|
2013-11-18 19:18:03 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
catch (const Exception & e)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2015-06-29 21:35:35 +00:00
|
|
|
stream << "Code: " << e.code() << ", e.displayText() = " << e.displayText() << ", e.what() = " << e.what();
|
|
|
|
|
|
|
|
if (with_stacktrace)
|
|
|
|
stream << ", Stack trace:\n\n" << e.getStackTrace().toString();
|
2013-11-18 19:18:03 +00:00
|
|
|
}
|
|
|
|
catch (...) {}
|
|
|
|
}
|
|
|
|
catch (const Poco::Exception & e)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2015-06-29 21:35:35 +00:00
|
|
|
stream << "Poco::Exception. Code: " << ErrorCodes::POCO_EXCEPTION << ", e.code() = " << e.code()
|
|
|
|
<< ", e.displayText() = " << e.displayText() << ", e.what() = " << e.what();
|
2013-11-18 19:18:03 +00:00
|
|
|
}
|
|
|
|
catch (...) {}
|
|
|
|
}
|
|
|
|
catch (const std::exception & e)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2015-03-06 16:47:35 +00:00
|
|
|
int status = 0;
|
|
|
|
auto name = demangle(typeid(e).name(), status);
|
2014-07-21 09:11:20 +00:00
|
|
|
|
|
|
|
if (status)
|
|
|
|
name += " (demangling status: " + toString(status) + ")";
|
|
|
|
|
2015-06-29 21:35:35 +00:00
|
|
|
stream << "std::exception. Code: " << ErrorCodes::STD_EXCEPTION << ", type: " << name << ", e.what() = " << e.what();
|
2013-11-18 19:18:03 +00:00
|
|
|
}
|
|
|
|
catch (...) {}
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2015-03-06 16:47:35 +00:00
|
|
|
int status = 0;
|
|
|
|
auto name = demangle(abi::__cxa_current_exception_type()->name(), status);
|
|
|
|
|
|
|
|
if (status)
|
|
|
|
name += " (demangling status: " + toString(status) + ")";
|
|
|
|
|
2015-06-29 21:35:35 +00:00
|
|
|
stream << "Unknown exception. Code: " << ErrorCodes::UNKNOWN_EXCEPTION << ", type: " << name;
|
2013-11-18 19:18:03 +00:00
|
|
|
}
|
|
|
|
catch (...) {}
|
|
|
|
}
|
2015-06-29 21:35:35 +00:00
|
|
|
|
|
|
|
return stream.str();
|
2013-11-18 19:18:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-11-02 23:42:10 +00:00
|
|
|
void rethrowFirstException(Exceptions & exceptions)
|
|
|
|
{
|
|
|
|
for (size_t i = 0, size = exceptions.size(); i < size; ++i)
|
|
|
|
if (exceptions[i])
|
|
|
|
exceptions[i]->rethrow();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-01 16:59:51 +00:00
|
|
|
}
|