]> xenbits.xensource.com Git - people/liuw/libxenctrl-split/libvirt.git/commitdiff
Propagate libvirt errors back with python exceptions
authorDaniel P. Berrange <berrange@redhat.com>
Tue, 7 Nov 2006 23:18:56 +0000 (23:18 +0000)
committerDaniel P. Berrange <berrange@redhat.com>
Tue, 7 Nov 2006 23:18:56 +0000 (23:18 +0000)
ChangeLog
python/generator.py
python/libvir.c
python/libvir.py

index 4cf4c9b7b02b5cf3b7b7ce33907657f90d4ac3d4..7cdbe479579d9df2ab35d563c19dd7dd98f5984e 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+Tue Nov  7 16:17:23 EDT 2006 Daniel P. Berrange <berrange@redhat.com>
+
+       * python/generator.py: Pass in connection object when generating
+       an exception
+       * python/libvir.py: Allow raw error object to be passed into the
+       python exception object.
+       * python/libvir.c: Added binding for virGetLastError and
+       virConnGetLastError
+
 Tue Nov  7 15:58:43 EDT 2006 Daniel P. Berrange <berrange@redhat.com>
 
        * src/xend_internal.c: Ensure that virConnectPtr object is passed
index e973db193a238e28dc3521a0f231c9fbfae5a5a0..5aa2625544ccc7b77466c54fa4a2540f3bc7ff12 100755 (executable)
@@ -260,6 +260,8 @@ foreign_encoding_args = (
 # code is still automatically generated (so they are not in skip_function()).
 skip_impl = (
     'virConnectListDomainsID',
+    'virConnGetLastError',
+    'virGetLastError',
     'virDomainGetInfo',
     'virNodeGetInfo',
     'virDomainGetUUID',
@@ -869,9 +871,18 @@ def buildWrappers():
                            classes.write(
                                "        if ret is None:return None\n");
                        else:
-                           classes.write(
+                            if classname == "virConnect":
+                                classes.write(
+                    "        if ret is None:raise libvirtError('%s() failed', conn=self)\n" %
+                                              (name))
+                            elif classname == "virDomain":
+                                classes.write(
+                    "        if ret is None:raise libvirtError('%s() failed')\n" %
+                                              (name))
+                            else:
+                                classes.write(
                     "        if ret is None:raise libvirtError('%s() failed')\n" %
-                                         (name))
+                                              (name))
 
                        #
                        # generate the returned class wrapper for the object
index 3d580e58462b95dda9097aeff89d0bfac312effa..a7b98dc000d49af89f78efdfbd84f82bc9fc1a74 100644 (file)
@@ -18,6 +18,8 @@
 void initlibvirmod(void);
 
 PyObject *libvirt_virDomainGetUUID(PyObject *self ATTRIBUTE_UNUSED, PyObject *args);
+PyObject *libvirt_virGetLastError(PyObject *self ATTRIBUTE_UNUSED, PyObject *args);
+PyObject *libvirt_virConnGetLastError(PyObject *self ATTRIBUTE_UNUSED, PyObject *args);
 
 
 /************************************************************************
@@ -29,6 +31,62 @@ PyObject *libvirt_virDomainGetUUID(PyObject *self ATTRIBUTE_UNUSED, PyObject *ar
 static PyObject *libvirt_virPythonErrorFuncHandler = NULL;
 static PyObject *libvirt_virPythonErrorFuncCtxt = NULL;
 
+PyObject *
+libvirt_virGetLastError(PyObject *self ATTRIBUTE_UNUSED, PyObject *args ATTRIBUTE_UNUSED)
+{
+    virError err;
+    PyObject *info;
+
+    if (virCopyLastError(&err) <= 0) {
+        Py_INCREF(Py_None);
+       return(Py_None);
+    }
+
+    info = PyTuple_New(9);
+    PyTuple_SetItem(info, 0, PyInt_FromLong((long) err.code));
+    PyTuple_SetItem(info, 1, PyInt_FromLong((long) err.domain));
+    PyTuple_SetItem(info, 2, libvirt_constcharPtrWrap(err.message));
+    PyTuple_SetItem(info, 3, PyInt_FromLong((long) err.level));
+    PyTuple_SetItem(info, 4, libvirt_constcharPtrWrap(err.str1));
+    PyTuple_SetItem(info, 5, libvirt_constcharPtrWrap(err.str2));
+    PyTuple_SetItem(info, 6, libvirt_constcharPtrWrap(err.str3));
+    PyTuple_SetItem(info, 7, PyInt_FromLong((long) err.int1));
+    PyTuple_SetItem(info, 8, PyInt_FromLong((long) err.int2));
+
+    return info;
+}
+
+PyObject *
+libvirt_virConnGetLastError(PyObject *self ATTRIBUTE_UNUSED, PyObject *args)
+{
+    virError err;
+    PyObject *info;
+    virConnectPtr conn;
+    PyObject *pyobj_conn;
+
+    if (!PyArg_ParseTuple(args, (char *)"O:virConGetLastError", &pyobj_conn))
+        return(NULL);
+    conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
+
+    if (virConnCopyLastError(conn, &err) <= 0) {
+        Py_INCREF(Py_None);
+       return(Py_None);
+    }
+
+    info = PyTuple_New(9);
+    PyTuple_SetItem(info, 0, PyInt_FromLong((long) err.code));
+    PyTuple_SetItem(info, 1, PyInt_FromLong((long) err.domain));
+    PyTuple_SetItem(info, 2, libvirt_constcharPtrWrap(err.message));
+    PyTuple_SetItem(info, 3, PyInt_FromLong((long) err.level));
+    PyTuple_SetItem(info, 4, libvirt_constcharPtrWrap(err.str1));
+    PyTuple_SetItem(info, 5, libvirt_constcharPtrWrap(err.str2));
+    PyTuple_SetItem(info, 6, libvirt_constcharPtrWrap(err.str3));
+    PyTuple_SetItem(info, 7, PyInt_FromLong((long) err.int1));
+    PyTuple_SetItem(info, 8, PyInt_FromLong((long) err.int2));
+
+    return info;
+}
+
 static void
 libvirt_virErrorFuncHandler(ATTRIBUTE_UNUSED void *ctx, virErrorPtr err)
 {
@@ -311,6 +369,8 @@ static PyMethodDef libvirtMethods[] = {
     {(char *) "virDomainGetUUID", libvirt_virDomainGetUUID, METH_VARARGS, NULL},
     {(char *) "virDomainLookupByUUID", libvirt_virDomainLookupByUUID, METH_VARARGS, NULL},
     {(char *) "virRegisterErrorHandler", libvirt_virRegisterErrorHandler, METH_VARARGS, NULL},
+    {(char *) "virGetLastError", libvirt_virGetLastError, METH_VARARGS, NULL},
+    {(char *) "virConnGetLastError", libvirt_virConnGetLastError, METH_VARARGS, NULL},
     {NULL, NULL, 0, NULL}
 };
 
index 29dc8c41dea39ec762c97c4ecfccb651522d1e22..b0f0b2c1c880d7c58b96c142c3c24852eea7194e 100644 (file)
@@ -7,10 +7,63 @@
 import libvirtmod
 import types
 
-# The root of all libxml2 errors.
+# The root of all libvirt errors.
 class libvirtError(Exception):
-    pass
+    def __init__(self, msg, conn=None):
+        Exception.__init__(self, msg)
 
+        if conn is None:
+            self.err = virGetLastError()
+        else:
+            self.err = conn.virConnGetLastError()
+
+    def get_error_code(self):
+        if self.err is None:
+            return None
+        return self.err[0]
+
+    def get_error_domain(self):
+        if self.err is None:
+            return None
+        return self.err[1]
+
+    def get_error_message(self):
+        if self.err is None:
+            return None
+        return self.err[2]
+
+    def get_error_level(self):
+        if self.err is None:
+            return None
+        return self.err[3]
+
+    def get_str1(self):
+        if self.err is None:
+            return None
+        return self.err[4]
+
+    def get_str2(self):
+        if self.err is None:
+            return None
+        return self.err[5]
+
+    def get_str3(self):
+        if self.err is None:
+            return None
+        return self.err[6]
+
+    def get_int1(self):
+        if self.err is None:
+            return None
+        return self.err[7]
+
+    def get_int2(self):
+        if self.err is None:
+            return None
+        return self.err[8]
+
+    def __str__(self):
+        return Exception.__str__(self) + " " + self.get_error_message()
 
 #
 # register the libvirt global error handler