From dfafecaf0c74cc5b13bd9ba8b609765f827df9ca Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Mon, 30 Dec 2019 22:28:11 +0200 Subject: [PATCH] utils: add Mu::Error (runtime exception) --- lib/utils/Makefile.am | 1 + lib/utils/mu-error.hh | 82 +++++++++++++++++++++++++++++++++++++++++++ lib/utils/mu-utils.cc | 28 +++++++++------ 3 files changed, 101 insertions(+), 10 deletions(-) create mode 100644 lib/utils/mu-error.hh diff --git a/lib/utils/Makefile.am b/lib/utils/Makefile.am index d04c1010..da174cb3 100644 --- a/lib/utils/Makefile.am +++ b/lib/utils/Makefile.am @@ -38,6 +38,7 @@ noinst_LTLIBRARIES= \ libmu_utils_la_SOURCES= \ mu-date.c \ mu-date.h \ + mu-error.hh \ mu-log.c \ mu-log.h \ mu-str.c \ diff --git a/lib/utils/mu-error.hh b/lib/utils/mu-error.hh new file mode 100644 index 00000000..c69b368d --- /dev/null +++ b/lib/utils/mu-error.hh @@ -0,0 +1,82 @@ +/* +** Copyright (C) 2019 Dirk-Jan C. Binnema +** +** 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 +#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__ */ diff --git a/lib/utils/mu-utils.cc b/lib/utils/mu-utils.cc index fc7e3810..7a1629ea 100644 --- a/lib/utils/mu-utils.cc +++ b/lib/utils/mu-utils.cc @@ -181,21 +181,29 @@ Mu::quote (const std::string& str) va_list args; va_start (args, frm); - - char *s = {}; - const auto res = g_vasprintf (&s, frm, args); + auto str = format(frm, args); va_end (args); - if (res == -1) { - std::cerr << "string format failed" << std::endl; - return {}; - } - - std::string str = s; - free (s); 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 char InternalDateMin[] = "0000000000"; constexpr const char InternalDateMax[] = "9999999999";