[PATCH 1/3] Add a virGetLastErrorMessage() function

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

 



From: "Daniel P. Berrange" <berrange@xxxxxxxxxx>

Apps using libvirt will often have code like

   if (virXXXX() < 0) {
      virErrorPtr err = virGetLastError();
      fprintf(stderr, "Something failed: %s\n",
              err && err->message ? err->message :
              "unknown error");
      return -1;
   }

Checking for a NULL error object or message leads to very
verbose code. A virGetLastErrorMessage() helper from libvirt
can simplify this to

   if (virXXXX() < 0) {
      fprintf(stderr, "Something failed: %s\n",
              virGetLastErrorMessage());
      return -1;
   }

Signed-off-by: Daniel P. Berrange <berrange@xxxxxxxxxx>
---
 include/libvirt/virterror.h |  2 ++
 src/libvirt_public.syms     |  5 +++++
 src/util/virerror.c         | 21 +++++++++++++++++++++
 3 files changed, 28 insertions(+)

diff --git a/include/libvirt/virterror.h b/include/libvirt/virterror.h
index 3864a31..cd7e3b3 100644
--- a/include/libvirt/virterror.h
+++ b/include/libvirt/virterror.h
@@ -313,6 +313,8 @@ void			virResetLastError	(void);
 void			virResetError		(virErrorPtr err);
 void			virFreeError		(virErrorPtr err);
 
+const char *            virGetLastErrorMessage  (void);
+
 virErrorPtr		virConnGetLastError	(virConnectPtr conn);
 void			virConnResetLastError	(virConnectPtr conn);
 int			virCopyLastError	(virErrorPtr to);
diff --git a/src/libvirt_public.syms b/src/libvirt_public.syms
index dfbf44e..4ee2d27 100644
--- a/src/libvirt_public.syms
+++ b/src/libvirt_public.syms
@@ -616,4 +616,9 @@ LIBVIRT_1.0.5 {
         virNodeDeviceDetachFlags;
 } LIBVIRT_1.0.3;
 
+LIBVIRT_1.0.6 {
+    global:
+        virGetLastErrorMessage;
+} LIBVIRT_1.0.5;
+
 # .... define new API here using predicted next version number ....
diff --git a/src/util/virerror.c b/src/util/virerror.c
index af4da8c..4e1dc9a 100644
--- a/src/util/virerror.c
+++ b/src/util/virerror.c
@@ -234,6 +234,27 @@ virGetLastError(void)
     return err;
 }
 
+
+/**
+ * virGetLastErrorMessage:
+ *
+ * Get the most recent error message
+ *
+ * Returns the most recent error message string in this
+ * thread, or a generic message if none is set
+ */
+const char *
+virGetLastErrorMessage(void)
+{
+    virErrorPtr err = virLastErrorObject();
+    if (!err || err->code == VIR_ERR_OK)
+        return _("no error");
+    if (err->message == NULL)
+        return _("unknown error");
+    return err->message;
+}
+
+
 /**
  * virSetError:
  * @newerr: previously saved error object
-- 
1.8.2.1

--
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]