diff options
author | Markus Armbruster <armbru@redhat.com> | 2015-12-18 16:35:12 +0100 |
---|---|---|
committer | Timothy Pearson <tpearson@raptorengineering.com> | 2019-11-29 19:28:24 -0600 |
commit | 5d3cf3e91d9243f6f1cb5af05dba0331b1009b41 (patch) | |
tree | 835fdf83a1942e0ef036b31ffa8b9470bb7be954 /util/error.c | |
parent | 770e7b1aa4d9e060c184ca47d579eff1cce0c601 (diff) | |
download | hqemu-5d3cf3e91d9243f6f1cb5af05dba0331b1009b41.zip hqemu-5d3cf3e91d9243f6f1cb5af05dba0331b1009b41.tar.gz |
error: New error_prepend(), error_reportf_err()
Instead of simply propagating an error verbatim, we sometimes want to
add to its message, like this:
frobnicate(arg, &err);
error_setg(errp, "Can't frobnicate %s: %s",
arg, error_get_pretty(err));
error_free(err);
This is suboptimal, because it loses err's hint (if any). Moreover,
when errp is &error_abort or is subsequently propagated to
&error_abort, the abort message points to the place where we last
added to the error, not to the place where it originated.
To avoid these issues, provide means to add to an error's message in
place:
frobnicate(arg, errp);
error_prepend(errp, "Can't frobnicate %s: ", arg);
Likewise, reporting an error like
frobnicate(arg, &err);
error_report("Can't frobnicate %s: %s", arg, error_get_pretty(err));
can lose err's hint. To avoid:
error_reportf_err(err, "Can't frobnicate %s: ", arg);
The next commits will put these functions to use.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Message-Id: <1450452927-8346-10-git-send-email-armbru@redhat.com>
Diffstat (limited to 'util/error.c')
-rw-r--r-- | util/error.c | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/util/error.c b/util/error.c index ebfb74b..57303fd 100644 --- a/util/error.c +++ b/util/error.c @@ -122,6 +122,29 @@ void error_setg_file_open_internal(Error **errp, "Could not open '%s'", filename); } +void error_vprepend(Error **errp, const char *fmt, va_list ap) +{ + GString *newmsg; + + if (!errp) { + return; + } + + newmsg = g_string_new(NULL); + g_string_vprintf(newmsg, fmt, ap); + g_string_append(newmsg, (*errp)->msg); + (*errp)->msg = g_string_free(newmsg, 0); +} + +void error_prepend(Error **errp, const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + error_vprepend(errp, fmt, ap); + va_end(ap); +} + void error_append_hint(Error **errp, const char *fmt, ...) { va_list ap; @@ -209,6 +232,16 @@ void error_report_err(Error *err) error_free(err); } +void error_reportf_err(Error *err, const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + error_vprepend(&err, fmt, ap); + va_end(ap); + error_report_err(err); +} + void error_free(Error *err) { if (err) { |