utils: Update error exception, utils.

This commit is contained in:
Dirk-Jan C. Binnema
2020-01-18 13:38:41 +02:00
parent 419871862c
commit a3d71dab91
3 changed files with 80 additions and 12 deletions

View File

@ -149,9 +149,8 @@ struct Store::Private {
// very basic check; just ensure there's no ',' in the address. // very basic check; just ensure there's no ',' in the address.
// we don't insist on full RFC5322 // we don't insist on full RFC5322
if (addr.find(",") != std::string::npos) if (addr.find(",") != std::string::npos)
throw Mu::Error::make(Error::Code::InvalidArgument, throw Mu::Error(Error::Code::InvalidArgument,
"e-mail address '%s' contains comma", "e-mail address '%s' contains comma", addr.c_str());
addr.c_str());
if (!all_addresses.empty()) if (!all_addresses.empty())
all_addresses += ','; all_addresses += ',';
all_addresses += addr; all_addresses += addr;
@ -607,7 +606,7 @@ mu_store_get_docid_for_path (const MuStore *store, const char* path, GError **er
Xapian::MSet mset (enq.get_mset (0,1)); Xapian::MSet mset (enq.get_mset (0,1));
if (mset.empty()) if (mset.empty())
throw Mu::Error::make(Error::Code::NotFound, throw Mu::Error(Error::Code::NotFound,
"message @ %s not found in store", path); "message @ %s not found in store", path);
return *mset.begin(); return *mset.begin();

View File

@ -23,17 +23,24 @@
#include <stdexcept> #include <stdexcept>
#include "mu-utils.hh" #include "mu-utils.hh"
#include <glib.h>
namespace Mu { namespace Mu {
struct Error final: public std::runtime_error { struct Error final: public std::exception {
enum struct Code { enum struct Code {
AccessDenied, AccessDenied,
Command,
File, File,
Index,
Internal, Internal,
InvalidArgument, InvalidArgument,
NotFound, NotFound,
Parsing,
Query,
SchemaMismatch, SchemaMismatch,
Store,
}; };
/** /**
@ -43,10 +50,9 @@ struct Error final: public std::runtime_error {
* #param msgarg the error diecription * #param msgarg the error diecription
*/ */
Error(Code codearg, const std::string& msgarg): 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 * 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 * @return an Error object
*/ */
__attribute__((format(printf, 2, 0))) __attribute__((format(printf, 3, 0)))
static Error make(Code codearg, const char *frm, ...) { Error(Code codearg, const char *frm, ...): code_{codearg} {
va_list args; va_list args;
va_start(args, frm); va_start(args, frm);
auto msg = format(frm, args); what_ = format(frm, args);
va_end(args); va_end(args);
return Error(codearg, msg); }
/**
* 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);
what_ = format(frm, args);
va_end(args);
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_; } Code code() const { return code_; }
private: private:
const Code code_; const Code code_;
std::string what_;
}; };

View File

@ -21,6 +21,7 @@
#define __MU_UTILS_HH__ #define __MU_UTILS_HH__
#include <string> #include <string>
#include <sstream>
#include <vector> #include <vector>
#include <cstdarg> #include <cstdarg>
#include <glib.h> #include <glib.h>
@ -136,6 +137,24 @@ std::string size_to_string (const std::string& sizestr, bool first);
std::string size_to_string (int64_t size); 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... * don't repeat these catch blocks everywhere...