27 #include <string_view>
29 #include <fmt/format.h>
35 class Error :
public std::exception {
37 Error(std::string_view what) : err_(what.data()) {}
39 template <
typename... Args>
40 Error(std::string_view format, Args&&... args)
41 : err_(fmt::format(fmt::runtime(format), std::forward<Args>(args)...)) {}
43 virtual ~
Error() noexcept =
default;
45 const char* what()
const noexcept
override {
return err_.what(); }
47 bool has_explanation()
const {
return !explanation_.empty(); }
49 void set_explanation(std::string explanation);
51 template <
typename... Args>
52 void set_explanation(std::string_view format, Args&&... args) {
53 set_explanation(fmt::format(fmt::runtime(format), std::forward<Args>(args)...));
56 const std::string& explanation()
const {
return explanation_; }
58 Error explanation(std::string explanation) && {
59 set_explanation(std::move(explanation));
60 return std::move(*
this);
63 template <
typename... Args>
64 Error explanation(std::string_view format, Args&&... args) && {
65 set_explanation(fmt::format(fmt::runtime(format), std::forward<Args>(args)...));
66 return std::move(*
this);
70 std::runtime_error err_;
71 std::string explanation_;
85 std::exception& cause()
const noexcept {
return cause_; }
86 const char* what()
const noexcept
override {
return cause_.what(); }
89 std::exception& cause_;