[libvirt] PATCH: Improve python exception messages

Daniel P. Berrange berrange at redhat.com
Tue Aug 19 09:31:34 UTC 2008


Most of the libvirt python API bindings use code snippet like this when
raising an exception:

    if ret is None:raise libvirtError('virConnectOpen() failed')


THis sets the message associated with the exception to

    "virConnectOpen() failed"

This contains essentially zero useful information - you can see that it
was virConnectOpen which failed from the stack trace.

Now the libvirt error object has a real message, such as

   "authentication failed"

Or

   "unable to connect to '/var/run/libvirt/libvirt-sock': Connection refused"

This patch makes sure we extract this real error message and use it to 
set the message associated with the exception object. This is one step
in getting better error reporting for virt-install/virt-manager, which
is particularly needed for remote connections

Daniel

diff -u -r1.9 libvir.py
--- python/libvir.py	11 Jun 2008 07:49:01 -0000	1.9
+++ python/libvir.py	18 Aug 2008 12:07:03 -0000
@@ -13,10 +13,22 @@
 
 import types
 
+def _getErrorMessage(conn, defmsg):
+    err = None
+    if conn is None:
+        err = virGetLastError()
+    else:
+        err = conn.virConnGetLastError()
+    if err is None:
+        return defmsg
+    else:
+        return err[2]
+
+
 # The root of all libvirt errors.
 class libvirtError(Exception):
     def __init__(self, msg, conn=None, dom=None, net=None, pool=None, vol=None):
-        Exception.__init__(self, msg)
+        Exception.__init__(self, _getErrorMessage(conn, msg))
 
         if dom is not None:
             conn = dom._conn
@@ -77,12 +89,6 @@
             return None
         return self.err[8]
 
-    def __str__(self):
-        if self.get_error_message() is None:
-            return Exception.__str__(self)
-        else:
-            return Exception.__str__(self) + " " + self.get_error_message()
-
 #
 # register the libvirt global error handler
 #


-- 
|: Red Hat, Engineering, London   -o-   http://people.redhat.com/berrange/ :|
|: http://libvirt.org  -o-  http://virt-manager.org  -o-  http://ovirt.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: GnuPG: 7D3B9505  -o-  F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|




More information about the libvir-list mailing list