]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
util: error: Add API for prefixing last set error with a string
authorPeter Krempa <pkrempa@redhat.com>
Tue, 11 Jun 2019 09:23:48 +0000 (11:23 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Thu, 20 Jun 2019 15:10:24 +0000 (17:10 +0200)
In some cases we report a low level error message which does not have
enough information to see what the problem is. To allow improving on
this add an API which will prefix the error message with another error
message string which can be used to describe where the error comes from.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
ACKed-by: Michal Privoznik <mprivozn@redhat.com>
cfg.mk
src/libvirt_private.syms
src/util/virerror.c
src/util/virerror.h

diff --git a/cfg.mk b/cfg.mk
index c0c240b2c0bf240f8ea5ff07f235c8865be4982a..f59cfd6e29799be5bd38dc8c0be41d4d3eda5741 100644 (file)
--- a/cfg.mk
+++ b/cfg.mk
@@ -614,6 +614,7 @@ msg_gen_function += virReportError
 msg_gen_function += virReportErrorHelper
 msg_gen_function += virReportSystemError
 msg_gen_function += xenapiSessionErrorHandler
+msg_gen_function += virLastErrorPrefixMessage
 
 # Uncomment the following and run "make syntax-check" to see diagnostics
 # that are not yet marked for translation, but that need to be rewritten
index c06fa67d6aae28d252b7d36864634085532a4362..48df6bce0855bc99a4afb1c2d2674ddca8456acf 100644 (file)
@@ -1832,6 +1832,7 @@ virErrorPreserveLast;
 virErrorRestore;
 virErrorSetErrnoFromLastError;
 virLastErrorIsSystemErrno;
+virLastErrorPrefixMessage;
 virRaiseErrorFull;
 virRaiseErrorObject;
 virReportErrorHelper;
index 67e2bc984e700256a588a182e95571b2dfa5de19..dfba8c5712384126888b884e94b668cbda1eb5e7 100644 (file)
@@ -1461,3 +1461,41 @@ bool virLastErrorIsSystemErrno(int errnum)
         return false;
     return true;
 }
+
+
+/**
+ * virLastErrorPrefixMessage:
+ * @fmt: printf-style formatting string
+ * @...: Arguments for @fmt
+ *
+ * Prefixes last error reported with message formatted from @fmt. This is useful
+ * if the low level error message does not convey enough information to describe
+ * the problem.
+ */
+void
+virLastErrorPrefixMessage(const char *fmt, ...)
+{
+    int save_errno = errno;
+    virErrorPtr err = virGetLastError();
+    VIR_AUTOFREE(char *) fmtmsg = NULL;
+    VIR_AUTOFREE(char *) newmsg = NULL;
+    va_list args;
+
+    if (!err)
+        return;
+
+    va_start(args, fmt);
+
+    if (virVasprintfQuiet(&fmtmsg, fmt, args) < 0)
+        goto cleanup;
+
+    if (virAsprintfQuiet(&newmsg, "%s: %s", fmtmsg, err->message) < 0)
+        goto cleanup;
+
+    VIR_FREE(err->message);
+    VIR_STEAL_PTR(err->message, newmsg);
+
+ cleanup:
+    va_end(args);
+    errno = save_errno;
+}
index 4cfb0c18dd91c2b5d6d51119a7b078621b10fb30..fa88217b274b4ee75de28ef20a5854d27f11f0f4 100644 (file)
@@ -205,4 +205,7 @@ bool virLastErrorIsSystemErrno(int errnum);
 void virErrorPreserveLast(virErrorPtr *saveerr);
 void virErrorRestore(virErrorPtr *savederr);
 
+void virLastErrorPrefixMessage(const char *fmt, ...)
+    ATTRIBUTE_FMT_PRINTF(1, 2);
+
 VIR_DEFINE_AUTOPTR_FUNC(virError, virFreeError);