ClickHouse/src/Common/Exception.h

217 lines
6.7 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/Version.h>
2015-10-05 01:26:43 +00:00
#include <Poco/Exception.h>
2019-07-29 22:26:44 +00:00
#include <Common/StackTrace.h>
2015-10-05 01:26:43 +00:00
#include <fmt/format.h>
2020-10-16 11:58:47 +00:00
#if !defined(NDEBUG) || defined(ADDRESS_SANITIZER) || defined(THREAD_SANITIZER) || defined(MEMORY_SANITIZER) || defined(UNDEFINED_BEHAVIOR_SANITIZER)
#define ABORT_ON_LOGICAL_ERROR
#endif
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:
2020-03-08 21:04:10 +00:00
Exception() = default;
Exception(const std::string & msg, int code, bool remote_ = false);
Exception(const std::string & msg, const Exception & nested, int code);
Exception(int code, const std::string & message)
: Exception(message, code)
{}
// Format message with fmt::format, like the logging functions.
template <typename ...Args>
Exception(int code, const std::string & fmt, Args&&... args)
: Exception(fmt::format(fmt, std::forward<Args>(args)...), code)
{}
struct CreateFromPocoTag {};
struct CreateFromSTDTag {};
Exception(CreateFromPocoTag, const Poco::Exception & exc);
Exception(CreateFromSTDTag, const std::exception & exc);
2018-11-22 18:09:17 +00:00
Exception * clone() const override { return new Exception(*this); }
void rethrow() const override { throw *this; }
2018-11-22 18:09:17 +00:00
const char * name() const throw() override { return "DB::Exception"; }
2019-02-04 14:28:38 +00:00
const char * what() const throw() override { return message().data(); }
2020-10-01 14:29:22 +00:00
/// Add something to the existing message.
template <typename ...Args>
void addMessage(const std::string& format, Args&&... args)
{
extendedMessage(fmt::format(format, std::forward<Args>(args)...));
}
void addMessage(const std::string& message)
2020-10-01 11:02:53 +00:00
{
extendedMessage(message);
2020-10-01 11:02:53 +00:00
}
/// Used to distinguish local exceptions from the one that was received from remote node.
void setRemoteException(bool remote_ = true) { remote = remote_; }
bool isRemoteException() const { return remote; }
std::string getStackTraceString() const;
2015-10-05 01:26:43 +00:00
private:
#ifndef STD_EXCEPTION_HAS_STACK_TRACE
2019-07-01 22:11:11 +00:00
StackTrace trace;
#endif
bool remote = false;
2018-11-22 18:09:17 +00:00
const char * className() const throw() override { return "DB::Exception"; }
2015-10-05 01:26:43 +00:00
};
std::string getExceptionStackTraceString(const std::exception & e);
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:
2019-08-06 18:54:06 +00:00
ErrnoException(const std::string & msg, int code, int saved_errno_, const std::optional<std::string> & path_ = {})
: Exception(msg, code), saved_errno(saved_errno_), path(path_) {}
2018-11-22 18:09:17 +00:00
ErrnoException * clone() const override { return new ErrnoException(*this); }
void rethrow() const override { throw *this; }
int getErrno() const { return saved_errno; }
2019-08-06 18:54:06 +00:00
const std::optional<std::string> getPath() const { return path; }
2015-10-05 01:26:43 +00:00
private:
int saved_errno;
2019-08-06 18:54:06 +00:00
std::optional<std::string> path;
2018-11-22 18:09:17 +00:00
const char * name() const throw() override { return "DB::ErrnoException"; }
const char * className() const throw() override { return "DB::ErrnoException"; }
2015-10-05 01:26:43 +00:00
};
2015-10-05 01:31:57 +00:00
2020-12-11 20:57:16 +00:00
/// Special class of exceptions, used mostly in ParallelParsingInputFormat for
2020-12-24 10:48:10 +00:00
/// more convenient calculation of problem line number.
2020-12-10 18:02:40 +00:00
class ParsingException : public Exception
2020-12-10 17:26:36 +00:00
{
public:
2020-12-18 01:47:24 +00:00
ParsingException();
ParsingException(const std::string & msg, int code);
ParsingException(int code, const std::string & message);
2020-12-22 22:33:07 +00:00
2020-12-18 01:47:24 +00:00
// Format message with fmt::format, like the logging functions.
template <typename ...Args>
ParsingException(int code, const std::string & fmt, Args&&... args)
: Exception(fmt::format(fmt, std::forward<Args>(args)...), code)
{}
2020-12-18 00:17:26 +00:00
2020-12-10 17:26:36 +00:00
std::string displayText() const
#if defined(POCO_CLICKHOUSE_PATCH)
override
#endif
;
2020-12-10 17:26:36 +00:00
2020-12-10 18:02:40 +00:00
int getLineNumber() { return line_number_; }
void setLineNumber(int line_number) { line_number_ = line_number;}
2020-12-10 17:26:36 +00:00
private:
2020-12-18 00:17:26 +00:00
ssize_t line_number_{-1};
2020-12-10 17:26:36 +00:00
mutable std::string formatted_message_;
const char * name() const throw() override { return "DB::ParsingException"; }
const char * className() const throw() override { return "DB::ParsingException"; }
};
using Exceptions = std::vector<std::exception_ptr>;
2015-10-05 01:31:57 +00:00
[[noreturn]] void throwFromErrno(const std::string & s, int code, int the_errno = errno);
2020-04-23 18:00:43 +00:00
/// Useful to produce some extra information about available space and inodes on device
2019-08-07 12:52:47 +00:00
[[noreturn]] void throwFromErrnoWithPath(const std::string & s, const std::string & path, int code,
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.
2020-02-15 00:11:09 +00:00
* with_extra_info - add information about the filesystem in case of "No space left on device" and similar.
2017-02-07 16:36:12 +00:00
*/
2019-08-05 19:41:20 +00:00
std::string getCurrentExceptionMessage(bool with_stacktrace, bool check_embedded_stacktrace = false,
2019-08-06 12:51:10 +00:00
bool with_extra_info = true);
2017-02-07 16:36:12 +00:00
/// 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
}