#pragma once #include #include #include #include #include #include #if !defined(NDEBUG) || defined(ADDRESS_SANITIZER) || defined(THREAD_SANITIZER) || defined(MEMORY_SANITIZER) || defined(UNDEFINED_BEHAVIOR_SANITIZER) #define ABORT_ON_LOGICAL_ERROR #endif namespace Poco { class Logger; } namespace DB { class Exception : public Poco::Exception { public: Exception() = default; Exception(const std::string & msg, int code); 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 Exception(int code, const std::string & fmt, Args&&... args) : Exception(fmt::format(fmt, std::forward(args)...), code) {} struct CreateFromPocoTag {}; struct CreateFromSTDTag {}; Exception(CreateFromPocoTag, const Poco::Exception & exc); Exception(CreateFromSTDTag, const std::exception & exc); Exception * clone() const override { return new Exception(*this); } void rethrow() const override { throw *this; } const char * name() const throw() override { return "DB::Exception"; } const char * what() const throw() override { return message().data(); } /// Add something to the existing message. template void addMessage(const std::string& format, Args&&... args) { extendedMessage(fmt::format(format, std::forward(args)...)); } void addMessage(const std::string& message) { extendedMessage(message); } std::string getStackTraceString() const; private: #ifndef STD_EXCEPTION_HAS_STACK_TRACE StackTrace trace; #endif const char * className() const throw() override { return "DB::Exception"; } }; std::string getExceptionStackTraceString(const std::exception & e); /// Contains an additional member `saved_errno`. See the throwFromErrno function. class ErrnoException : public Exception { public: ErrnoException(const std::string & msg, int code, int saved_errno_, const std::optional & path_ = {}) : Exception(msg, code), saved_errno(saved_errno_), path(path_) {} ErrnoException * clone() const override { return new ErrnoException(*this); } void rethrow() const override { throw *this; } int getErrno() const { return saved_errno; } const std::optional getPath() const { return path; } private: int saved_errno; std::optional path; const char * name() const throw() override { return "DB::ErrnoException"; } const char * className() const throw() override { return "DB::ErrnoException"; } }; /// Special class of exceptions, used mostly in ParallelParsingInputFormat for /// more convinient calculation of problem line number. class ParsingException : public Exception { public: using Exception::Exception; /// We use additional field formatted_message_ to make this method const. std::string displayText() const override { try { formatted_message_ = fmt::format(message(), line_number_); } catch (...) {} if (!formatted_message_.empty()) { std::string result = name(); result.append(": "); result.append(formatted_message_); return result; } else { return Exception::displayText(); } } int getLineNumber() { return line_number_; } void setLineNumber(int line_number) { line_number_ = line_number;} private: int line_number_; 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; [[noreturn]] void throwFromErrno(const std::string & s, int code, int the_errno = errno); /// Useful to produce some extra information about available space and inodes on device [[noreturn]] void throwFromErrnoWithPath(const std::string & s, const std::string & path, int code, int the_errno = errno); /** Try to write an exception to the log (and forget about it). * Can be used in destructors in the catch-all block. */ void tryLogCurrentException(const char * log_name, const std::string & start_of_message = ""); void tryLogCurrentException(Poco::Logger * logger, const std::string & start_of_message = ""); /** 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. * with_extra_info - add information about the filesystem in case of "No space left on device" and similar. */ std::string getCurrentExceptionMessage(bool with_stacktrace, bool check_embedded_stacktrace = false, bool with_extra_info = true); /// Returns error code from ErrorCodes int getCurrentExceptionCode(); /// 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); bool tryDeserializeText(const std::string & data); }; 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); std::string getExceptionMessage(std::exception_ptr e, bool with_stacktrace); void rethrowFirstException(const Exceptions & exceptions); template std::enable_if_t, T> exception_cast(std::exception_ptr e) { try { std::rethrow_exception(std::move(e)); } catch (std::remove_pointer_t & concrete) { return &concrete; } catch (...) { return nullptr; } } }