summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDirk-Jan C. Binnema <djcb@djcbsoftware.nl>2023-04-11 20:34:31 +0300
committerDirk-Jan C. Binnema <djcb@djcbsoftware.nl>2023-04-15 08:02:35 +0300
commita9e96ff9358dc993cbde6c3a4ecaf19d4a2b27cf (patch)
tree1350d7a08d0b1c24cc360d1bb3e060446dc76b1e
parent79035e7487b4c129ce6999c6d791b5c357b84893 (diff)
mu: improve error numbers
Ensure they match what mu expects.
-rw-r--r--lib/mu-server.cc12
-rw-r--r--lib/utils/mu-error.hh87
-rw-r--r--mu/mu-cmd-server.cc2
3 files changed, 57 insertions, 44 deletions
diff --git a/lib/mu-server.cc b/lib/mu-server.cc
index 50087ad..58f2bcb 100644
--- a/lib/mu-server.cc
+++ b/lib/mu-server.cc
@@ -33,6 +33,7 @@
#include <cstring>
#include <glib.h>
#include <glib/gprintf.h>
+#include <unistd.h>
#include "mu-maildir.hh"
#include "mu-query.hh"
@@ -338,18 +339,15 @@ Server::Private::make_command_map()
G_GNUC_PRINTF(2, 3)
static Sexp
-make_error(Error::Code errcode, const char* frm, ...)
+make_error(Error::Code code, const char* frm, ...)
{
- char* msg{};
va_list ap;
-
va_start(ap, frm);
- g_vasprintf(&msg, frm, ap);
+ auto err = Sexp().put_props(
+ ":error", Error::error_number(code),
+ ":message", vformat(frm, ap));
va_end(ap);
- auto err = Sexp().put_props(":error", static_cast<int>(errcode),
- ":message", msg);
- g_free(msg);
return err;
}
diff --git a/lib/utils/mu-error.hh b/lib/utils/mu-error.hh
index 2962aba..562d7c4 100644
--- a/lib/utils/mu-error.hh
+++ b/lib/utils/mu-error.hh
@@ -22,6 +22,7 @@
#include <stdexcept>
#include <string>
+#include <errno.h>
#include <cstdint>
#include "mu-utils-format.hh"
@@ -29,47 +30,52 @@
namespace Mu {
+// calculate an error enum value.
+constexpr uint32_t err_enum(uint8_t code, uint8_t rv, uint8_t cat) {
+ return static_cast<uint32_t>(code|(rv << 16)|cat<<24);
+}
+
struct Error final : public std::exception {
// 16 lower bits are for the error code the next 8 bits is for the return code
// upper byte is for flags
-
- static constexpr uint32_t SoftError = 1 << 23;
-
-#define ERROR_ENUM(RV,CAT) \
- static_cast<uint32_t>(__LINE__ | ((RV) << 15) | (CAT))
+ static constexpr uint8_t SoftError = 1;
enum struct Code: uint32_t {
- AccessDenied = ERROR_ENUM(1,0),
- AssertionFailure = ERROR_ENUM(1,0),
- Command = ERROR_ENUM(1,0),
- Crypto = ERROR_ENUM(1,0),
- File = ERROR_ENUM(1,0),
- Index = ERROR_ENUM(1,0),
- Internal = ERROR_ENUM(1,0),
- InvalidArgument = ERROR_ENUM(1,0),
- Message = ERROR_ENUM(1,0),
- NoMatches = ERROR_ENUM(2,SoftError),
- NotFound = ERROR_ENUM(1,0),
- Parsing = ERROR_ENUM(1,0),
- Play = ERROR_ENUM(1,0),
- Query = ERROR_ENUM(1,0),
- SchemaMismatch = ERROR_ENUM(11,0),
- Script = ERROR_ENUM(1,0),
- ScriptNotFound = ERROR_ENUM(1,0),
- Store = ERROR_ENUM(1,0),
- StoreLock = ERROR_ENUM(19,0),
- UnverifiedSignature = ERROR_ENUM(1,0),
- User = ERROR_ENUM(1,0),
- Xapian = ERROR_ENUM(1,0),
+ Ok = err_enum(0,0,0),
+
+ // used by mu4e.
+ NoMatches = err_enum(4,2,SoftError),
+ SchemaMismatch = err_enum(110,11,0),
+
+ // other
+ AccessDenied = err_enum(100,1,0),
+ AssertionFailure = err_enum(101,1,0),
+ Command = err_enum(102,1,0),
+ Crypto = err_enum(103,1,0),
+ File = err_enum(104,1,0),
+ Index = err_enum(105,1,0),
+ Internal = err_enum(106,1,0),
+ InvalidArgument = err_enum(107,1,0),
+ Message = err_enum(108,1,0),
+ NotFound = err_enum(109,1,0),
+ Parsing = err_enum(111,1,0),
+ Play = err_enum(112,1,0),
+ Query = err_enum(113,1,0),
+ Script = err_enum(115,1,0),
+ ScriptNotFound = err_enum(116,1,0),
+ Store = err_enum(117,1,0),
+ StoreLock = err_enum(118,19,0),
+ UnverifiedSignature = err_enum(119,1,0),
+ User = err_enum(120,1,0),
+ Xapian = err_enum(121,1,0),
};
-
/**
* Construct an error
*
* @param codearg error-code
- * #param msgarg the error diecription
+ * @param msgarg the error description
*/
Error(Code codearg, const std::string& msgarg) : code_{codearg}, what_{msgarg} {}
Error(Code codearg, std::string&& msgarg) : code_{codearg}, what_{std::move(msgarg)} {}
@@ -92,7 +98,7 @@ struct Error final : public std::exception {
va_end(args);
}
- Error(Error&& rhs) = default;
+ Error(Error&& rhs) = default;
Error(const Error& rhs) = default;
/**
@@ -142,6 +148,16 @@ struct Error final : public std::exception {
*/
Code code() const noexcept { return code_; }
+ /**
+ * Get the error number (e.g. for reporting to mu4e) for some error.
+ *
+ * @param c error code
+ *
+ * @return the error number
+ */
+ static constexpr uint32_t error_number(Code c) noexcept {
+ return static_cast<uint32_t>(c) & 0xffff;
+ }
/**
* Is this is a 'soft error'?
@@ -153,10 +169,9 @@ struct Error final : public std::exception {
}
constexpr uint8_t exit_code() const {
- return ((static_cast<uint32_t>(code_) >> 15) & 0xff);
+ return ((static_cast<uint32_t>(code_) >> 16) & 0xff);
}
-
/**
* Fill a GError with the error information
*
@@ -169,10 +184,10 @@ struct Error final : public std::exception {
private:
static inline GQuark error_quark (void) {
- static GQuark error_domain = 0;
- if (G_UNLIKELY(error_domain == 0))
- error_domain = g_quark_from_static_string("mu-error-quark");
- return error_domain;
+ static GQuark error_domain = 0;
+ if (G_UNLIKELY(error_domain == 0))
+ error_domain = g_quark_from_static_string("mu-error-quark");
+ return error_domain;
}
const Code code_;
diff --git a/mu/mu-cmd-server.cc b/mu/mu-cmd-server.cc
index 3537470..b25635f 100644
--- a/mu/mu-cmd-server.cc
+++ b/mu/mu-cmd-server.cc
@@ -103,7 +103,7 @@ output_sexp_stdout(const Sexp& sexp, Server::OutputFlags flags)
static void
report_error(const Mu::Error& err) noexcept
{
- output_sexp_stdout(Sexp(":error"_sym, static_cast<size_t>(err.code()),
+ output_sexp_stdout(Sexp(":error"_sym, Error::error_number(err.code()),
":message"_sym, err.what()),
Server::OutputFlags::Flush);
}