2019-12-30 21:28:11 +01:00
|
|
|
/*
|
|
|
|
** Copyright (C) 2019 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
|
|
|
**
|
|
|
|
** This program is free software; you can redistribute it and/or modify it
|
|
|
|
** under the terms of the GNU General Public License as published by the
|
|
|
|
** Free Software Foundation; either version 3, or (at your option) any
|
|
|
|
** later version.
|
|
|
|
**
|
|
|
|
** This program is distributed in the hope that it will be useful,
|
|
|
|
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
** GNU General Public License for more details.
|
|
|
|
**
|
|
|
|
** You should have received a copy of the GNU General Public License
|
|
|
|
** along with this program; if not, write to the Free Software Foundation,
|
|
|
|
** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef MU_ERROR_HH__
|
|
|
|
#define MU_ERROR_HH__
|
|
|
|
|
|
|
|
#include <stdexcept>
|
|
|
|
#include "mu-utils.hh"
|
2020-01-18 12:38:41 +01:00
|
|
|
#include <glib.h>
|
2019-12-30 21:28:11 +01:00
|
|
|
|
|
|
|
namespace Mu {
|
|
|
|
|
2020-01-18 12:38:41 +01:00
|
|
|
struct Error final: public std::exception {
|
|
|
|
|
2019-12-30 21:28:11 +01:00
|
|
|
enum struct Code {
|
|
|
|
AccessDenied,
|
2020-01-18 12:38:41 +01:00
|
|
|
Command,
|
2020-01-05 00:15:07 +01:00
|
|
|
File,
|
2020-01-18 12:38:41 +01:00
|
|
|
Index,
|
2019-12-30 21:28:11 +01:00
|
|
|
Internal,
|
|
|
|
InvalidArgument,
|
|
|
|
NotFound,
|
2020-01-18 12:38:41 +01:00
|
|
|
Parsing,
|
|
|
|
Query,
|
2019-12-30 21:28:11 +01:00
|
|
|
SchemaMismatch,
|
2020-01-18 12:38:41 +01:00
|
|
|
Store,
|
2019-12-30 21:28:11 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct an error
|
|
|
|
*
|
|
|
|
* @param codearg error-code
|
|
|
|
* #param msgarg the error diecription
|
|
|
|
*/
|
|
|
|
Error(Code codearg, const std::string& msgarg):
|
2020-01-18 12:38:41 +01:00
|
|
|
code_{codearg}, what_{msgarg}
|
2019-12-30 21:28:11 +01:00
|
|
|
{}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build an error from an error-code and a format string
|
|
|
|
*
|
|
|
|
* @param code error-code
|
|
|
|
* @param frm format string
|
|
|
|
* @param ... format parameters
|
|
|
|
*
|
|
|
|
* @return an Error object
|
|
|
|
*/
|
2020-01-18 12:38:41 +01:00
|
|
|
__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);
|
|
|
|
}
|
|
|
|
|
2020-01-18 12:35:01 +01:00
|
|
|
Error(Error&& rhs) = default;
|
|
|
|
Error(const Error& rhs) = delete;
|
|
|
|
|
2020-01-18 12:38:41 +01:00
|
|
|
/**
|
|
|
|
* 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} {
|
|
|
|
|
2019-12-30 21:28:11 +01:00
|
|
|
va_list args;
|
|
|
|
va_start(args, frm);
|
2020-01-18 12:38:41 +01:00
|
|
|
what_ = format(frm, args);
|
2019-12-30 21:28:11 +01:00
|
|
|
va_end(args);
|
2020-01-18 12:38:41 +01:00
|
|
|
|
|
|
|
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();
|
2019-12-30 21:28:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the error-code for this error
|
|
|
|
*
|
|
|
|
* @return the error-code
|
|
|
|
*/
|
|
|
|
Code code() const { return code_; }
|
|
|
|
|
2020-01-18 12:38:41 +01:00
|
|
|
|
|
|
|
|
2019-12-30 21:28:11 +01:00
|
|
|
private:
|
|
|
|
const Code code_;
|
2020-01-18 12:38:41 +01:00
|
|
|
std::string what_;
|
|
|
|
|
2019-12-30 21:28:11 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace Mu
|
|
|
|
|
|
|
|
|
|
|
|
#endif /* MU_ERROR_HH__ */
|