[PATCH 1/2] util: error: Add API for prefixing last set error with a string

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxx>
---
 cfg.mk                   |  1 +
 src/libvirt_private.syms |  1 +
 src/util/virerror.c      | 38 ++++++++++++++++++++++++++++++++++++++
 src/util/virerror.h      |  3 +++
 4 files changed, 43 insertions(+)

diff --git a/cfg.mk b/cfg.mk
index 5074ef611a..f99b0d0b55 100644
--- 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
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 8ee76645cd..1ded794931 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1810,6 +1810,7 @@ virErrorPreserveLast;
 virErrorRestore;
 virErrorSetErrnoFromLastError;
 virLastErrorIsSystemErrno;
+virLastErrorPrefixMessage;
 virRaiseErrorFull;
 virRaiseErrorObject;
 virReportErrorHelper;
diff --git a/src/util/virerror.c b/src/util/virerror.c
index 37b5b2f3f9..7c5f4da8e8 100644
--- a/src/util/virerror.c
+++ b/src/util/virerror.c
@@ -1452,3 +1452,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;
+}
diff --git a/src/util/virerror.h b/src/util/virerror.h
index 8f51510dc2..ca875fb8f4 100644
--- a/src/util/virerror.h
+++ b/src/util/virerror.h
@@ -206,6 +206,9 @@ 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);

 #endif /* LIBVIRT_VIRERROR_H */
-- 
2.21.0

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list



[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux