14 template <
typename TBase,
typename TException>
17 std::optional<Exception> m_inner;
25 TBase(
fmt::format("{0}
", typeid(TException).name())) { }
26 explicit ExceptionBase(Exception&& inner) noexcept :
27 TBase(fmt::format("{0}\r\n\t{1}
", typeid(TException).name(), inner.what())), m_inner(std::move(inner)) { }
28 explicit ExceptionBase(std::string_view message) noexcept :
29 TBase(fmt::format("{0}: {1}
", typeid(TException).name(), message)) { }
30 explicit ExceptionBase(Exception&& inner, std::string_view message) noexcept :
31 TBase(fmt::format("{0}: {1}\r\n\t{2}
", typeid(TException).name(), message, inner.what())), m_inner(std::move(inner)) { }
33 template <typename ...TArgs>
34 explicit ExceptionBase(std::string_view format, TArgs&&... args) noexcept :
35 TBase(fmt::format("{0}: {1}
", typeid(TException).name(), fmt::format(fmt::runtime(format), std::forward<TArgs>(args)...))) { }
37 template <typename ...TArgs>
38 explicit ExceptionBase(Exception&& inner, std::string_view format, TArgs&&... args) noexcept :
39 TBase(fmt::format("{0}: {1}\r\n\t{2}
", typeid(TException).name(), fmt::format(fmt::runtime(format), std::forward<TArgs>(args)...), inner.what())) { }
43 virtual const Exception* innerException() const noexcept {
44 // NOTE: For some reason, initializing an std::optional by moving the exception into it results in a copy that drops the inherited exception. Be aware, that
45 // you only get an std::exception instance here.
46 return m_inner.has_value() ? &m_inner.value() : nullptr;
50#define DEFINE_EXCEPTION(name, base) class name : public ExceptionBase<base, name> { \
52 using ExceptionBase<base, name>::ExceptionBase; \
55 DEFINE_EXCEPTION(InvalidArgumentException, std::invalid_argument);
56 DEFINE_EXCEPTION(ArgumentOutOfRangeException, std::out_of_range);
57 DEFINE_EXCEPTION(ArgumentNotInitializedException, std::logic_error);
58 DEFINE_EXCEPTION(RuntimeException, std::runtime_error);
Definition: exceptions.hpp:15
ExceptionBase(const ExceptionBase &)=delete
ExceptionBase(ExceptionBase &&)=delete
virtual ~ExceptionBase() noexcept=default
std::exception Exception
Definition: exceptions.hpp:12
Definition: app_formatters.hpp:6