[libvirt] PATCH: Improve python exception messages

Daniel P. Berrange berrange at redhat.com
Thu Aug 21 11:00:46 UTC 2008


On Tue, Aug 19, 2008 at 10:31:34AM +0100, Daniel P. Berrange wrote:
> 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

This patch was flawed - it missed out the error message info when passed
in a domain/network/volume/pool instead of a connection object. Here is
an updated patch which addresses that


Daniel

Index: python/libvir.py
===================================================================
RCS file: /data/cvs/libvirt/python/libvir.py,v
retrieving revision 1.9
diff -u -r1.9 libvir.py
--- python/libvir.py	11 Jun 2008 07:49:01 -0000	1.9
+++ python/libvir.py	21 Aug 2008 10:58:53 -0000
@@ -15,8 +15,7 @@
 
 # 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)
+    def __init__(self, defmsg, conn=None, dom=None, net=None, pool=None, vol=None):
 
         if dom is not None:
             conn = dom._conn
@@ -28,9 +27,17 @@
             conn = vol._conn
 
         if conn is None:
-            self.err = virGetLastError()
+            err = virGetLastError()
+        else:
+            err = conn.virConnGetLastError()
+        if err is None:
+            msg = defmsg
         else:
-            self.err = conn.virConnGetLastError()
+            msg = err[2]
+
+        Exception.__init__(self, msg)
+
+        self.err = err
 
     def get_error_code(self):
         if self.err is None:
@@ -77,12 +84,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