ClickHouse/dbms/src/Common/Exception.h

132 lines
4.1 KiB
C++
Raw Normal View History

2015-10-05 01:26:43 +00:00
#pragma once
#include <cerrno>
#include <vector>
#include <memory>
2015-10-05 01:26:43 +00:00
#include <Poco/Exception.h>
#include <Common/StackTrace.h>
2015-10-05 01:26:43 +00:00
2015-10-05 01:31:57 +00:00
namespace Poco { class Logger; }
2015-10-05 01:26:43 +00:00
namespace DB
{
class Exception : public Poco::Exception
{
public:
Exception() {} /// For deferred initialization.
Exception(const std::string & msg, int code = 0) : Poco::Exception(msg, code) {}
Exception(const std::string & msg, const std::string & arg, int code = 0) : Poco::Exception(msg, arg, code) {}
Exception(const std::string & msg, const Exception & exc, int code = 0) : Poco::Exception(msg, exc, code), trace(exc.trace) {}
explicit Exception(const Poco::Exception & exc) : Poco::Exception(exc.displayText()) {}
const char * name() const throw() override { return "DB::Exception"; }
const char * className() const throw() override { return "DB::Exception"; }
DB::Exception * clone() const override { return new DB::Exception(*this); }
void rethrow() const override { throw *this; }
2017-05-07 20:25:26 +00:00
/// Add something to the existing message.
void addMessage(const std::string & arg) { extendedMessage(arg); }
const StackTrace & getStackTrace() const { return trace; }
2015-10-05 01:26:43 +00:00
private:
StackTrace trace;
2015-10-05 01:26:43 +00:00
};
2017-05-07 20:25:26 +00:00
/// Contains an additional member `saved_errno`. See the throwFromErrno function.
2015-10-05 01:26:43 +00:00
class ErrnoException : public Exception
{
public:
ErrnoException(const std::string & msg, int code = 0, int saved_errno_ = 0)
: Exception(msg, code), saved_errno(saved_errno_) {}
ErrnoException(const std::string & msg, const std::string & arg, int code = 0, int saved_errno_ = 0)
: Exception(msg, arg, code), saved_errno(saved_errno_) {}
ErrnoException(const std::string & msg, const Exception & exc, int code = 0, int saved_errno_ = 0)
: Exception(msg, exc, code), saved_errno(saved_errno_) {}
int getErrno() const { return saved_errno; }
2015-10-05 01:26:43 +00:00
private:
int saved_errno;
2015-10-05 01:26:43 +00:00
};
2015-10-05 01:31:57 +00:00
using Exceptions = std::vector<std::exception_ptr>;
2015-10-05 01:31:57 +00:00
[[noreturn]] void throwFromErrno(const std::string & s, int code = 0, int the_errno = errno);
2015-10-05 01:31:57 +00:00
2017-05-07 20:25:26 +00:00
/** Try to write an exception to the log (and forget about it).
* Can be used in destructors in the catch-all block.
2015-10-05 01:31:57 +00:00
*/
void tryLogCurrentException(const char * log_name, const std::string & start_of_message = "");
void tryLogCurrentException(Poco::Logger * logger, const std::string & start_of_message = "");
2017-02-07 16:36:12 +00:00
/** Prints current exception in canonical format.
* with_stacktrace - prints stack trace for DB::Exception.
* check_embedded_stacktrace - if DB::Exception has embedded stacktrace then
* only this stack trace will be printed.
*/
std::string getCurrentExceptionMessage(bool with_stacktrace, bool check_embedded_stacktrace = false);
/// Returns error code from ErrorCodes
int getCurrentExceptionCode();
2015-10-05 01:31:57 +00:00
2017-05-31 14:01:08 +00:00
/// An execution status of any piece of code, contains return code and optional error
struct ExecutionStatus
{
int code = 0;
std::string message;
ExecutionStatus() = default;
explicit ExecutionStatus(int return_code, const std::string & exception_message = "")
: code(return_code), message(exception_message) {}
static ExecutionStatus fromCurrentException(const std::string & start_of_message = "");
std::string serializeText() const;
void deserializeText(const std::string & data);
2017-07-27 13:11:16 +00:00
bool tryDeserializeText(const std::string & data);
};
2015-10-05 05:40:27 +00:00
void tryLogException(std::exception_ptr e, const char * log_name, const std::string & start_of_message = "");
void tryLogException(std::exception_ptr e, Poco::Logger * logger, const std::string & start_of_message = "");
std::string getExceptionMessage(const Exception & e, bool with_stacktrace, bool check_embedded_stacktrace = false);
2015-10-05 05:40:27 +00:00
std::string getExceptionMessage(std::exception_ptr e, bool with_stacktrace);
2016-06-08 14:39:30 +00:00
void rethrowFirstException(const Exceptions & exceptions);
2015-10-05 01:31:57 +00:00
2015-10-05 05:40:27 +00:00
template <typename T>
2017-12-25 04:01:46 +00:00
std::enable_if_t<std::is_pointer_v<T>, T> exception_cast(std::exception_ptr e)
2015-10-05 05:40:27 +00:00
{
try
{
std::rethrow_exception(std::move(e));
}
2017-12-25 04:01:46 +00:00
catch (std::remove_pointer_t<T> & concrete)
{
return &concrete;
}
catch (...)
{
return nullptr;
}
2015-10-05 05:40:27 +00:00
}
2015-10-05 01:26:43 +00:00
}