From 3123f3e983b816f52b85597f94851e080879033e Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Sat, 16 Sep 2023 11:01:51 +0300 Subject: [PATCH] mu-error: allow for adding end-user hints --- lib/utils/mu-error.hh | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/utils/mu-error.hh b/lib/utils/mu-error.hh index 29be7e5f..c038719f 100644 --- a/lib/utils/mu-error.hh +++ b/lib/utils/mu-error.hh @@ -137,7 +137,7 @@ struct Error final : public std::exception { * @return true or false */ constexpr bool is_soft_error() const { - return (static_cast(code_) & SoftError) != 0; + return !!((static_cast(code_)>>24) & SoftError); } constexpr uint8_t exit_code() const { @@ -154,6 +154,26 @@ struct Error final : public std::exception { "%s", what_.c_str()); } + /** + * Add an end-user hint + * + * @param args... libfmt-style format string and parameters + * + * @return the error + */ + template + Error& add_hint(fmt::format_string frm, T&&... args) { + hint_ = fmt::format(frm, std::forward(args)...); + return *this; + } + + /** + * Get the hint + * + * @return the hint, empty for no hint. + */ + const std::string& hint() const { return hint_; } + private: static inline GQuark error_quark (void) { static GQuark error_domain = 0; @@ -164,9 +184,9 @@ private: const Code code_; const std::string what_; + std::string hint_; }; - static inline auto format_as(const Error& err) { return mu_format("<{} ({})>", err.what(), Error::error_number(err.code()));