mirror of https://github.com/djcb/mu.git
utils: add Mu::Error (runtime exception)
This commit is contained in:
parent
4dc35247d1
commit
dfafecaf0c
|
@ -38,6 +38,7 @@ noinst_LTLIBRARIES= \
|
||||||
libmu_utils_la_SOURCES= \
|
libmu_utils_la_SOURCES= \
|
||||||
mu-date.c \
|
mu-date.c \
|
||||||
mu-date.h \
|
mu-date.h \
|
||||||
|
mu-error.hh \
|
||||||
mu-log.c \
|
mu-log.c \
|
||||||
mu-log.h \
|
mu-log.h \
|
||||||
mu-str.c \
|
mu-str.c \
|
||||||
|
|
|
@ -0,0 +1,82 @@
|
||||||
|
/*
|
||||||
|
** 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"
|
||||||
|
|
||||||
|
namespace Mu {
|
||||||
|
|
||||||
|
struct Error final: public std::runtime_error {
|
||||||
|
enum struct Code {
|
||||||
|
AccessDenied,
|
||||||
|
Internal,
|
||||||
|
InvalidArgument,
|
||||||
|
NotFound,
|
||||||
|
SchemaMismatch,
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct an error
|
||||||
|
*
|
||||||
|
* @param codearg error-code
|
||||||
|
* #param msgarg the error diecription
|
||||||
|
*/
|
||||||
|
Error(Code codearg, const std::string& msgarg):
|
||||||
|
std::runtime_error(msgarg), code_{codearg}
|
||||||
|
{}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
__attribute__((format(printf, 2, 0)))
|
||||||
|
static Error make(Code codearg, const char *frm, ...) {
|
||||||
|
va_list args;
|
||||||
|
va_start(args, frm);
|
||||||
|
auto msg = format(frm, args);
|
||||||
|
va_end(args);
|
||||||
|
return Error(codearg, msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the error-code for this error
|
||||||
|
*
|
||||||
|
* @return the error-code
|
||||||
|
*/
|
||||||
|
Code code() const { return code_; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
const Code code_;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
} // namespace Mu
|
||||||
|
|
||||||
|
|
||||||
|
#endif /* MU_ERROR_HH__ */
|
|
@ -181,21 +181,29 @@ Mu::quote (const std::string& str)
|
||||||
va_list args;
|
va_list args;
|
||||||
|
|
||||||
va_start (args, frm);
|
va_start (args, frm);
|
||||||
|
auto str = format(frm, args);
|
||||||
char *s = {};
|
|
||||||
const auto res = g_vasprintf (&s, frm, args);
|
|
||||||
va_end (args);
|
va_end (args);
|
||||||
if (res == -1) {
|
|
||||||
std::cerr << "string format failed" << std::endl;
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string str = s;
|
|
||||||
free (s);
|
|
||||||
|
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string
|
||||||
|
Mu::format (const char *frm, va_list args)
|
||||||
|
{
|
||||||
|
char *s{};
|
||||||
|
const auto res = g_vasprintf (&s, frm, args);
|
||||||
|
if (res == -1) {
|
||||||
|
std::cerr << "string format failed" << std::endl;
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string str{s};
|
||||||
|
g_free (s);
|
||||||
|
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
constexpr const auto InternalDateFormat = "%010" G_GINT64_FORMAT;
|
constexpr const auto InternalDateFormat = "%010" G_GINT64_FORMAT;
|
||||||
constexpr const char InternalDateMin[] = "0000000000";
|
constexpr const char InternalDateMin[] = "0000000000";
|
||||||
constexpr const char InternalDateMax[] = "9999999999";
|
constexpr const char InternalDateMax[] = "9999999999";
|
||||||
|
|
Loading…
Reference in New Issue