diff options
| author | Dirk-Jan C. Binnema <djcb@djcbsoftware.nl> | 2020-01-18 13:38:41 +0200 |
|---|---|---|
| committer | Dirk-Jan C. Binnema <djcb@djcbsoftware.nl> | 2020-01-18 13:40:15 +0200 |
| commit | a3d71dab91f77a6dc5d1b9d1974c1ca98852b879 (patch) | |
| tree | f8cf445e0ca7b8c8226914f94b429369b56fb139 /lib/utils | |
| parent | 419871862c6beb62d47d2a8bceb1e6e59080682e (diff) | |
utils: Update error exception, utils.
Diffstat (limited to 'lib/utils')
| -rw-r--r-- | lib/utils/mu-error.hh | 64 | ||||
| -rw-r--r-- | lib/utils/mu-utils.hh | 19 |
2 files changed, 76 insertions, 7 deletions
diff --git a/lib/utils/mu-error.hh b/lib/utils/mu-error.hh index 18692c9..991e71d 100644 --- a/lib/utils/mu-error.hh +++ b/lib/utils/mu-error.hh @@ -23,17 +23,24 @@ #include <stdexcept> #include "mu-utils.hh" +#include <glib.h> namespace Mu { -struct Error final: public std::runtime_error { +struct Error final: public std::exception { + enum struct Code { AccessDenied, + Command, File, + Index, Internal, InvalidArgument, NotFound, + Parsing, + Query, SchemaMismatch, + Store, }; /** @@ -43,10 +50,9 @@ struct Error final: public std::runtime_error { * #param msgarg the error diecription */ Error(Code codearg, const std::string& msgarg): - std::runtime_error(msgarg), code_{codearg} + code_{codearg}, what_{msgarg} {} - /** * Build an error from an error-code and a format string * @@ -56,13 +62,53 @@ struct Error final: public std::runtime_error { * * @return an Error object */ - __attribute__((format(printf, 2, 0))) - static Error make(Code codearg, const char *frm, ...) { + __attribute__((format(printf, 3, 0))) + Error(Code codearg, const char *frm, ...): code_{codearg} { + va_list args; + va_start(args, frm); + what_ = format(frm, args); + va_end(args); + } + + /** + * Build an error from a GError an error-code and a format string + * + * @param code error-code + * @param gerr a GError or {}, which is consumed + * @param frm format string + * @param ... format parameters + * + * @return an Error object + */ + __attribute__((format(printf, 4, 0))) + Error(Code codearg, GError **err, const char *frm, ...): code_{codearg} { + va_list args; va_start(args, frm); - auto msg = format(frm, args); + what_ = format(frm, args); va_end(args); - return Error(codearg, msg); + + if (err && *err) + what_ += format (": %s", (*err)->message); + else + what_ += ": something went wrong"; + + g_clear_error(err); + } + + /** + * DTOR + * + */ + virtual ~Error() = default; + + /** + * Get the descriptiove message. + * + * @return + */ + virtual const char* what() const noexcept override { + return what_.c_str(); } /** @@ -72,8 +118,12 @@ struct Error final: public std::runtime_error { */ Code code() const { return code_; } + + private: const Code code_; + std::string what_; + }; diff --git a/lib/utils/mu-utils.hh b/lib/utils/mu-utils.hh index 77ca897..f5b2e61 100644 --- a/lib/utils/mu-utils.hh +++ b/lib/utils/mu-utils.hh @@ -21,6 +21,7 @@ #define __MU_UTILS_HH__ #include <string> +#include <sstream> #include <vector> #include <cstdarg> #include <glib.h> @@ -137,6 +138,24 @@ std::string size_to_string (int64_t size); /** + * Convert any ostreamable<< value to a string + * + * @param t the value + * + * @return a std::string + */ +template <typename T> +static inline std::string to_string (const T& val) +{ + std::stringstream sstr; + sstr << val; + + return sstr.str(); +} + + + +/** * * don't repeat these catch blocks everywhere... * |
