Skip to contentSkip to conversation

hmp: Allow for error message hints on HMP

PATCH Superseded Eric Blake 2 participants 3 messages Aug 18, 2015 – Aug 19, 2015

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Eric Blake

author
#0 lore raw

PATCH v2 1/1 hmp: Allow for error message hints on HMP

Commits 7216ae3d and d2828429 disabled some error message hints, all because a change to use modern error reporting meant that the hint would be output prior to the actual error. Fix this by making hints a first-class member of Error.

For example, we are now back to the pleasant:

 $ qemu-system-x86_64 --nodefaults -S --vnc :0 --chardev null,id=,
 qemu-system-x86_64: --chardev null,id=,: Parameter 'id' expects an identifier
 Identifiers consist of letters, digits, '-', '.', '_', starting with a letter.

---

v2: use GString instead of rolling our own O(n^2) string concat [Paolo]

 include/qapi/error.h |  7 +++++++
 qdev-monitor.c       | 42 ++++++++++++++----------------------------
 util/error.c         | 30 ++++++++++++++++++++++++++++++
 util/qemu-option.c   | 11 ++++-------
 4 files changed, 55 insertions(+), 35 deletions(-)
include/qapi/error.h +7 -0
index f44c451..00daea3 100644
@@ -67,6 +67,13 @@ void error_set_win32(Error **errp, int win32_err, ErrorClass err_class,
  */
 void error_setg_file_open(Error **errp, int os_errno, const char *filename);
 
+/**
+ * Append a printf-style human-readable explanation to an existing error.
+ * May be called multiple times, and safe if @errp is NULL.
+ */
+void error_append_hint(Error **errp, const char *fmt, ...)
+    GCC_FMT_ATTR(2, 3);
+
 /*
  * Get the error class of an error object.
  */
qdev-monitor.c +14 -28

84 lines not on this page · raw message

util/error.c +30 -0
index 14f4351..82cab49 100644
@@ -18,6 +18,7 @@ struct Error
 {
     char *msg;
     ErrorClass err_class;
+    GString *hint;
 };
 
 Error *error_abort;
@@ -91,6 +92,28 @@ void error_setg_file_open(Error **errp, int os_errno, const char *filename)
     error_setg_errno(errp, os_errno, "Could not open '%s'", filename);
 }
 
+void error_append_hint(Error **errp, const char *fmt, ...)
+{
+    va_list ap;
+    int saved_errno = errno;
+    Error *err;
+
+    if (!errp) {
+        return;
+    }
+    err = *errp;
+    assert(err && errp != &error_abort);
+
+    if (!err->hint) {
+        err->hint = g_string_new(NULL);
+    }
+    va_start(ap, fmt);
+    g_string_append_vprintf(err->hint, fmt, ap);
+    va_end(ap);
+
+    errno = saved_errno;
+}
+
 #ifdef _WIN32
 
 void error_set_win32(Error **errp, int win32_err, ErrorClass err_class,
@@ -138,6 +161,9 @@ Error *error_copy(const Error *err)
     err_new = g_malloc0(sizeof(*err));
     err_new->msg = g_strdup(err->msg);
     err_new->err_class = err->err_class;
+    if (err->hint) {
+        err_new->hint = g_string_new(err->hint->str);
+    }
 
     return err_new;
 }
@@ -155,6 +181,9 @@ const char *error_get_pretty(Error *err)
 void error_report_err(Error *err)
 {
     error_report("%s", error_get_pretty(err));
+    if (err->hint) {
+        error_printf_unless_qmp("%s\n", err->hint->str);
+    }
     error_free(err);
 }
 
@@ -162,6 +191,7 @@ void error_free(Error *err)
 {
     if (err) {
         g_free(err->msg);
+        g_string_free(err->hint, true);
         g_free(err);
     }
 }
util/qemu-option.c +4 -7
index efe9d27..03201ad 100644
@@ -200,10 +200,8 @@ void parse_option_size(const char *name, const char *value,
             break;
         default:
             error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name, "a size");
-#if 0 /* conversion from qerror_report() to error_set() broke this: */
-            error_printf_unless_qmp("You may use k, M, G or T suffixes for "
-                    "kilobytes, megabytes, gigabytes and terabytes.\n");
-#endif
+            error_append_hint(errp, "You may use k, M, G or T suffixes for "
+                    "kilobytes, megabytes, gigabytes and terabytes.");
             return;
         }
     } else {
@@ -643,9 +641,8 @@ QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id,
         if (!id_wellformed(id)) {
             error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "id",
                        "an identifier");
-#if 0 /* conversion from qerror_report() to error_set() broke this: */
-            error_printf_unless_qmp("Identifiers consist of letters, digits, '-', '.', '_', starting with a letter.\n");
-#endif
+            error_append_hint(errp, "Identifiers consist of letters, digits, "
+                              "'-', '.', '_', starting with a letter.");
             return NULL;
         }
         opts = qemu_opts_find(list, id);
Signature · 1 line
2.4.3
Reviewed-by on v2 1/1
#1 lore raw
Quoted 5 lines · Eric Blake

Commits 7216ae3d and d2828429 disabled some error message hints, all because a change to use modern error reporting meant that the hint would be output prior to the actual error. Fix this by making hints a first-class member of Error.

Quoted 8 lines

For example, we are now back to the pleasant:

 $ qemu-system-x86_64 --nodefaults -S --vnc :0 --chardev null,id=,
 qemu-system-x86_64: --chardev null,id=,: Parameter 'id' expects an identifier
 Identifiers consist of letters, digits, '-', '.', '_', starting with a letter.

---

author
#2 lore raw
In reply to Eric Blake
Quoted 18 lines · Eric Blake

Commits 7216ae3d and d2828429 disabled some error message hints, all because a change to use modern error reporting meant that the hint would be output prior to the actual error. Fix this by making hints a first-class member of Error.

For example, we are now back to the pleasant:

 $ qemu-system-x86_64 --nodefaults -S --vnc :0 --chardev null,id=,
 qemu-system-x86_64: --chardev null,id=,: Parameter 'id' expects an identifier
 Identifiers consist of letters, digits, '-', '.', '_', starting with a letter.

---

v2: use GString instead of rolling our own O(n^2) string concat [Paolo]

util/error.c void error_free(Error *err) · line 194

Quoted 4 lines
util/error.c +1 -0
@@ -162,6 +191,7 @@ void error_free(Error *err)
 {
     if (err) {
         g_free(err->msg);
+        g_string_free(err->hint, true);

self-NAK; v3 coming up. g_string_free(NULL) doesn't work (I hate "free" functions that aren't free-like).

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org