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

Re: [Ovirt-devel] [patch] Even Better Exceptions For ruby-libvirt bindings



Mohammed Morsi wrote:
I figured out how to add extra information to the exceptions, so from here on out we should be able to inform the client of whatever it needs to determine if it should continue processing. Right now, besides the standard message that ruby exceptions expect, the invoker's method_name" (previously just printed out) and "vir_connect_ptr" are added as attributes to the base ruby-libvirt exception. Enjoy!


------------------------------------------------------------------------

_______________________________________________
Ovirt-devel mailing list
Ovirt-devel redhat com
https://www.redhat.com/mailman/listinfo/ovirt-devel
My mistake, I ran "diff" manually, instead of "hg diff", attached is the corrected patch


diff -r b3dd1c9e9026 ext/libvirt/_libvirt.c
--- a/ext/libvirt/_libvirt.c	Fri Mar 28 11:33:43 2008 -0700
+++ b/ext/libvirt/_libvirt.c	Mon Mar 31 16:35:35 2008 -0400
@@ -17,7 +17,7 @@
  * License along with this library; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
  *
- * Author: David Lutterkort <dlutter redhat com>
+ * Authors: David Lutterkort <dlutter redhat com>, Mohammed Morsi <mmorsi redhat com>
  */
 
 #include <ruby.h>
@@ -41,6 +41,14 @@ static VALUE c_storage_vol;
 static VALUE c_storage_vol;
 static VALUE c_storage_vol_info;
 #endif
+
+// define additional errors here
+static VALUE e_Error;                   // Error - generic error
+static VALUE e_ConnectionError;         // ConnectionError - error durring connection establishment
+static VALUE e_DefinitionError;         // DefinitionError - error during data definition
+static VALUE e_RetrieveError;           // RetrievalError - error during data retrieval
+static VALUE e_OperationError;          // OperationError - error during other various operations
+
 
 /*
  * Internal helpers
@@ -72,6 +80,7 @@ static VALUE generic_new(VALUE klass, vo
     rb_iv_set(result, "@connection", conn);
     return result;
 }
+
 
 /* Connections */
 static void connect_close(void *p) {
@@ -110,6 +119,16 @@ static virConnectPtr conn(VALUE s) {
         rb_raise(rb_eArgError, "Connection has been closed");
     return conn;
 }
+
+/* Errors */
+static VALUE create_error(VALUE error, char* method, char* msg, 
+                                virConnectPtr conn){
+    extern VALUE ruby_errinfo;
+    ruby_errinfo = rb_exc_new2(error, msg);
+    rb_iv_set(ruby_errinfo, "@method_name", rb_str_new2(method));
+    rb_iv_set(ruby_errinfo, "@vir_connect_ptr", connect_new(conn));
+    return ruby_errinfo;
+};
 
 /* Domains */
 static void domain_free(void *d) {
@@ -170,13 +189,14 @@ static VALUE vol_new(virStorageVolPtr n,
 #endif
 
 /* Error handling */
-#define _E(cond, conn, fn) \
-    do { if (cond) vir_error(conn, fn); } while(0)
+#define _E(cond, excep) \
+    do { if (cond) vir_error(excep); } while(0)
 
-NORETURN(static void vir_error(virConnectPtr conn, const char *fn));
+NORETURN(static void vir_error(VALUE exception));
 
-static void vir_error(virConnectPtr conn, const char *fn) {
-    rb_raise(rb_eSystemCallError, "libvir call %s failed", fn);
+static void vir_error(VALUE exception) {
+    printf("libvir call %s failed\n", STR2CSTR(rb_iv_get(exception, "@method_name")));
+    rb_exc_raise(exception);
 }
 
 /*
@@ -197,7 +217,7 @@ static void vir_error(virConnectPtr conn
         virConnectPtr conn = connect_get(c);                            \
                                                                         \
         result = virConnectNumOf##objs(conn);                           \
-        _E(result < 0, conn, "virConnectNumOf" # objs);               \
+        _E(result < 0, create_error(e_RetrieveError, "virConnectNumOf" # objs, "", conn));                \
                                                                         \
         return INT2NUM(result);                                         \
     } while(0)
@@ -216,13 +236,13 @@ static void vir_error(virConnectPtr conn
         VALUE result;                                                   \
                                                                         \
         num = virConnectNumOf##objs(conn);                              \
-        _E(num < 0, conn, "virConnectNumOf" # objs);                  \
+        _E(num < 0, create_error(e_RetrieveError, "virConnectNumOf" # objs, "", conn));   \
                                                                         \
         names = ALLOC_N(char *, num);                                   \
         r = virConnectList##objs(conn, names, num);                     \
         if (r < 0) {                                                    \
             free(names);                                                \
-            _E(r < 0, conn, "virConnectList" # objs);                   \
+            _E(r < 0, create_error(e_RetrieveError, "virConnectList" # objs, "", conn));  \
         }                                                               \
                                                                         \
         result = rb_ary_new2(num);                                      \
@@ -242,7 +262,7 @@ static void vir_error(virConnectPtr conn
     do {                                                                \
         int _r_##func;                                                  \
         _r_##func = func(args);                                         \
-        _E(_r_##func < 0, conn, #func);                               \
+        _E(_r_##func < 0, create_error(e_Error, #func, "", conn));                        \
         return Qnil;                                                    \
     } while(0)
 
@@ -256,7 +276,7 @@ static void vir_error(virConnectPtr conn
         VALUE result;                                                   \
                                                                         \
         str = func(args);                                               \
-        _E(str == NULL, conn, # func);                                  \
+        _E(str == NULL, create_error(e_Error, # func, "", conn));                         \
                                                                         \
         result = rb_str_new2(str);                                      \
         if (dealloc)                                                    \
@@ -336,7 +356,7 @@ VALUE libvirt_open_read_only(VALUE m, VA
             rb_raise(rb_eTypeError, "expected string");
     }
     virConnectPtr ptr = virConnectOpenReadOnly(str);
-    _E(!ptr, NULL, "virConnectOpenReadOnly");
+    _E(!ptr, create_error(e_ConnectionError, "virConnectOpenReadOnly", "", NULL));
 
     return connect_new(ptr);
 }
@@ -396,7 +416,7 @@ VALUE libvirt_conn_version(VALUE s) {
     virConnectPtr conn = connect_get(s);
 
     r = virConnectGetVersion(conn, &v);
-    _E(r < 0, conn, "virConnectGetVersion");
+    _E(r < 0, create_error(e_RetrieveError, "virConnectGetVersion", "", conn));
 
     return ULONG2NUM(v);
 }
@@ -429,7 +449,7 @@ VALUE libvirt_conn_max_vcpus(VALUE s, VA
     virConnectPtr conn = connect_get(s);
 
     result = virConnectGetMaxVcpus(conn, StringValueCStr(type));
-    _E(result < 0, conn, "virConnectGetMaxVcpus");
+    _E(result < 0, create_error(e_RetrieveError, "virConnectGetMaxVcpus", "", conn));
 
     return INT2NUM(result);
 }
@@ -445,7 +465,7 @@ VALUE libvirt_conn_node_get_info(VALUE s
     VALUE modelstr;
 
     r = virNodeGetInfo(conn, &nodeinfo);
-    _E(r < 0, conn, "virNodeGetInfo");
+    _E(r < 0, create_error(e_RetrieveError, "virNodeGetInfo", "", conn));
 
     modelstr = rb_str_new2(nodeinfo.model);
 
@@ -486,13 +506,13 @@ VALUE libvirt_conn_list_domains(VALUE s)
     VALUE result;
 
     num = virConnectNumOfDomains(conn);
-    _E(num < 0, conn, "virConnectNumOfDomains");
+    _E(num < 0, create_error(e_RetrieveError, "virConnectNumOfDomains", "", conn));
 
     ids = ALLOC_N(int, num);
     r = virConnectListDomains(conn, ids, num);
     if (r < 0) {
         free(ids);
-        _E(r < 0, conn, "virConnectListDomains");
+        _E(r < 0, create_error(e_RetrieveError, "virConnectListDomains", "", conn));
     }
 
     result = rb_ary_new2(num);
@@ -660,7 +680,7 @@ VALUE libvirt_dom_info(VALUE s) {
     VALUE result;
 
     r = virDomainGetInfo(dom, &info);
-    _E(r < 0, conn(s), "virDomainGetInfo");
+    _E(r < 0, create_error(e_RetrieveError, "virDomainGetInfo", "", conn(s)));
 
     result = rb_class_new_instance(0, NULL, c_domain_info);
     rb_iv_set(result, "@state", CHR2FIX(info.state));
@@ -688,7 +708,7 @@ VALUE libvirt_dom_id(VALUE s) {
     unsigned int id;
 
     id = virDomainGetID(dom);
-    _E(id < 0, conn(s), "virDomainGetID");
+    _E(id < 0, create_error(e_RetrieveError, "virDomainGetID", "", conn(s)));
 
     return UINT2NUM(id);
 }
@@ -702,7 +722,7 @@ VALUE libvirt_dom_uuid(VALUE s) {
     int r;
 
     r = virDomainGetUUIDString(dom, uuid);
-    _E(r < 0, conn(s), "virDomainGetUUIDString");
+    _E(r < 0, create_error(e_RetrieveError, "virDomainGetUUIDString", "", conn(s)));
 
     return rb_str_new2((char *) uuid);
 }
@@ -723,7 +743,7 @@ VALUE libvirt_dom_max_memory(VALUE s) {
     unsigned long max_memory;
 
     max_memory = virDomainGetMaxMemory(dom);
-    _E(max_memory == 0, conn(s), "virDomainGetMaxMemory");
+    _E(max_memory == 0, create_error(e_RetrieveError, "virDomainGetMaxMemory", "", conn(s)));
 
     return ULONG2NUM(max_memory);
 }
@@ -736,7 +756,7 @@ VALUE libvirt_dom_max_memory_set(VALUE s
     int r;
 
     r = virDomainSetMaxMemory(dom, NUM2ULONG(max_memory));
-    _E(r < 0, conn(s), "virDomainSetMaxMemory");
+    _E(r < 0, create_error(e_DefinitionError, "virDomainSetMaxMemory", "", conn(s)));
 
     return ULONG2NUM(max_memory);
 }
@@ -749,7 +769,7 @@ VALUE libvirt_dom_memory_set(VALUE s, VA
     int r;
 
     r = virDomainSetMemory(dom, NUM2ULONG(memory));
-    _E(r < 0, conn(s), "virDomainSetMemory");
+    _E(r < 0, create_error(e_DefinitionError, "virDomainSetMemory", "", conn(s)));
 
     return ULONG2NUM(memory);
 }
@@ -762,7 +782,7 @@ VALUE libvirt_dom_max_vcpus(VALUE s) {
     int vcpus;
 
     vcpus = virDomainGetMaxVcpus(dom);
-    _E(vcpus < 0, conn(s), "virDomainGetMaxVcpus");
+    _E(vcpus < 0, create_error(e_RetrieveError, "virDomainGetMaxVcpus", "", conn(s)));
 
     return INT2NUM(vcpus);
 }
@@ -776,7 +796,37 @@ VALUE libvirt_dom_vcpus_set(VALUE s, VAL
     int r;
 
     r = virDomainSetVcpus(dom, NUM2UINT(nvcpus));
-    _E(r < 0, conn(s), "virDomainSetVcpus");
+    _E(r < 0, create_error(e_DefinitionError, "virDomainSetVcpus", "", conn(s)));
+
+    return r;
+}
+
+/*
+ * Call +virDomainPinVcpu+[http://www.libvirt.org/html/libvirt-libvirt.html#virDomainPinVcpu]
+ */
+VALUE libvirt_dom_pin_vcpu(VALUE s, VALUE vcpu, VALUE cpulist) {
+    virDomainPtr dom = domain_get(s);
+    int r, i, len, maplen;
+    unsigned char *cpumap;
+    virNodeInfo nodeinfo;
+    virConnectPtr c = conn(s);
+
+    r = virNodeGetInfo(c, &nodeinfo);
+    _E(r < 0, create_error(e_RetrieveError, "virNodeGetInfo", "", c));
+
+    maplen = VIR_CPU_MAPLEN(nodeinfo.cpus);
+    cpumap = ALLOC_N(unsigned char, maplen);
+    MEMZERO(cpumap, unsigned char, maplen);
+
+    len = RARRAY(cpulist)->len;
+    for(i = 0; i < len; i++) {
+        VALUE e = rb_ary_entry(cpulist, i);
+        VIR_USE_CPU(cpumap, NUM2UINT(e));
+    }
+
+    r = virDomainPinVcpu(dom, NUM2UINT(vcpu), cpumap, maplen);
+    free(cpumap);
+    _E(r < 0, create_error(e_OperationError, "virDomainPinVcpu", "", c));
 
     return r;
 }
@@ -814,7 +864,7 @@ VALUE libvirt_dom_autostart(VALUE s){
     int r, autostart;
 
     r = virDomainGetAutostart(dom, &autostart);
-    _E(r < 0, conn(s), "virDomainAutostart");
+    _E(r < 0, create_error(e_RetrieveError, "virDomainAutostart", "", conn(s)));
 
     return autostart ? Qtrue : Qfalse;
 }
@@ -838,7 +888,7 @@ VALUE libvirt_conn_create_linux(VALUE c,
     xmlDesc = StringValueCStr(xml);
 
     dom = virDomainCreateLinux(conn, xmlDesc, NUM2UINT(flags));
-    _E(dom == NULL, conn, "virDomainCreateLinux");
+    _E(dom == NULL, create_error(e_OperationError, "virDomainCreateLinux", "", conn));
 
     return domain_new(dom, c);
 }
@@ -851,7 +901,7 @@ VALUE libvirt_conn_lookup_domain_by_name
     virConnectPtr conn = connect_get(c);
 
     dom = virDomainLookupByName(conn, StringValueCStr(name));
-    _E(dom == NULL, conn, "virDomainLookupByName");
+    _E(dom == NULL, create_error(e_RetrieveError, "virDomainLookupByName", "", conn));
 
     return domain_new(dom, c);
 }
@@ -864,7 +914,7 @@ VALUE libvirt_conn_lookup_domain_by_id(V
     virConnectPtr conn = connect_get(c);
 
     dom = virDomainLookupByID(conn, NUM2INT(id));
-    _E(dom == NULL, conn, "virDomainLookupByID");
+    _E(dom == NULL, create_error(e_RetrieveError, "virDomainLookupByID", "", conn));
 
     return domain_new(dom, c);
 }
@@ -877,7 +927,7 @@ VALUE libvirt_conn_lookup_domain_by_uuid
     virConnectPtr conn = connect_get(c);
 
     dom = virDomainLookupByUUIDString(conn, StringValueCStr(uuid));
-    _E(dom == NULL, conn, "virDomainLookupByUUID");
+    _E(dom == NULL, create_error(e_RetrieveError, "virDomainLookupByUUID", "", conn));
 
     return domain_new(dom, c);
 }
@@ -890,7 +940,7 @@ VALUE libvirt_conn_define_domain_xml(VAL
     virConnectPtr conn = connect_get(c);
 
     dom = virDomainDefineXML(conn, StringValueCStr(xml));
-    _E(dom == NULL, conn, "virDomainDefineXML");
+    _E(dom == NULL, create_error(e_DefinitionError, "virDomainDefineXML", "", conn));
 
     return domain_new(dom, c);
 }
@@ -908,7 +958,7 @@ VALUE libvirt_conn_lookup_network_by_nam
     virConnectPtr conn = connect_get(c);
 
     netw = virNetworkLookupByName(conn, StringValueCStr(name));
-    _E(netw == NULL, conn, "virNetworkLookupByName");
+    _E(netw == NULL, create_error(e_RetrieveError, "virNetworkLookupByName", "", conn));
 
     return network_new(netw, c);
 }
@@ -921,7 +971,7 @@ VALUE libvirt_conn_lookup_network_by_uui
     virConnectPtr conn = connect_get(c);
 
     netw = virNetworkLookupByUUIDString(conn, StringValueCStr(uuid));
-    _E(netw == NULL, conn, "virNetworkLookupByUUID");
+    _E(netw == NULL, create_error(e_RetrieveError, "virNetworkLookupByUUID", "", conn));
 
     return network_new(netw, c);
 }
@@ -937,7 +987,7 @@ VALUE libvirt_conn_create_network_xml(VA
     xmlDesc = StringValueCStr(xml);
 
     netw = virNetworkCreateXML(conn, xmlDesc);
-    _E(netw == NULL, conn, "virNetworkCreateXML");
+    _E(netw == NULL, create_error(e_OperationError, "virNetworkCreateXML", "", conn));
 
     return network_new(netw, c);
 }
@@ -950,7 +1000,7 @@ VALUE libvirt_conn_define_network_xml(VA
     virConnectPtr conn = connect_get(c);
 
     netw = virNetworkDefineXML(conn, StringValueCStr(xml));
-    _E(netw == NULL, conn, "virNetworkDefineXML");
+    _E(netw == NULL, create_error(e_DefinitionError, "virNetworkDefineXML", "", conn));
 
     return network_new(netw, c);
 }
@@ -998,7 +1048,7 @@ VALUE libvirt_netw_uuid(VALUE s) {
     int r;
 
     r = virNetworkGetUUIDString(netw, uuid);
-    _E(r < 0, conn(s), "virNetworkGetUUIDString");
+    _E(r < 0, create_error(e_RetrieveError, "virNetworkGetUUIDString", "", conn(s)));
 
     return rb_str_new2((char *) uuid);
 }
@@ -1027,7 +1077,7 @@ VALUE libvirt_netw_autostart(VALUE s){
     int r, autostart;
 
     r = virNetworkGetAutostart(netw, &autostart);
-    _E(r < 0, conn(s), "virNetworkAutostart");
+    _E(r < 0, create_error(e_RetrieveError, "virNetworkAutostart", "", conn(s)));
 
     return autostart ? Qtrue : Qfalse;
 }
@@ -1054,7 +1104,7 @@ VALUE libvirt_conn_lookup_pool_by_name(V
     virConnectPtr conn = connect_get(c);
 
     pool = virStoragePoolLookupByName(conn, StringValueCStr(name));
-    _E(pool == NULL, conn, "virStoragePoolLookupByName");
+    _E(pool == NULL, create_error(e_RetrieveError, "virStoragePoolLookupByName", "", conn));
 
     return pool_new(pool, c);
 }
@@ -1067,7 +1117,7 @@ VALUE libvirt_conn_lookup_pool_by_uuid(V
     virConnectPtr conn = connect_get(c);
 
     pool = virStoragePoolLookupByUUIDString(conn, StringValueCStr(uuid));
-    _E(pool == NULL, conn, "virStoragePoolLookupByUUID");
+    _E(pool == NULL, create_error(e_RetrieveError, "virStoragePoolLookupByUUID", "", conn));
 
     return pool_new(pool, c);
 }
@@ -1079,7 +1129,7 @@ VALUE libvirt_vol_get_pool(VALUE v) {
     virStoragePoolPtr pool;
 
     pool = virStoragePoolLookupByVolume(vol_get(v));
-    _E(pool == NULL, conn(v), "virStoragePoolLookupByVolume");
+    _E(pool == NULL, create_error(e_RetrieveError, "virStoragePoolLookupByVolume", "", conn(v)));
 
     return pool_new(pool, conn_attr(v));
 }
@@ -1095,7 +1145,7 @@ VALUE libvirt_conn_create_pool_xml(VALUE
     xmlDesc = StringValueCStr(xml);
 
     pool = virStoragePoolCreateXML(conn, xmlDesc, NUM2UINT(flags));
-    _E(pool == NULL, conn, "virStoragePoolCreateXML");
+    _E(pool == NULL, create_error(e_OperationError, "virStoragePoolCreateXML", "", conn));
 
     return pool_new(pool, c);
 }
@@ -1108,7 +1158,7 @@ VALUE libvirt_conn_define_pool_xml(VALUE
     virConnectPtr conn = connect_get(c);
 
     pool = virStoragePoolDefineXML(conn, StringValueCStr(xml), NUM2UINT(flags));
-    _E(pool == NULL, conn, "virStoragePoolDefineXML");
+    _E(pool == NULL, create_error(e_DefinitionError, "virStoragePoolDefineXML", "", conn));
 
     return pool_new(pool, c);
 }
@@ -1168,7 +1218,7 @@ VALUE libvirt_pool_name(VALUE s) {
     const char *name;
 
     name = virStoragePoolGetName(pool_get(s));
-    _E(name == NULL, conn(s), "virStoragePoolGetName");
+    _E(name == NULL, create_error(e_RetrieveError, "virStoragePoolGetName", "", conn(s)));
 
     return rb_str_new2(name);
 }
@@ -1181,7 +1231,7 @@ VALUE libvirt_pool_uuid(VALUE s) {
     int r;
 
     r = virStoragePoolGetUUIDString(pool_get(s), uuid);
-    _E(r < 0, conn(s), "virStoragePoolGetUUIDString");
+    _E(r < 0, create_error(e_RetrieveError, "virStoragePoolGetUUIDString", "", conn(s)));
 
     return rb_str_new2((char *) uuid);
 }
@@ -1195,7 +1245,7 @@ VALUE libvirt_pool_info(VALUE s) {
     VALUE result;
 
     r = virStoragePoolGetInfo(pool_get(s), &info);
-    _E(r < 0, conn(s), "virStoragePoolGetInfo");
+    _E(r < 0, create_error(e_RetrieveError, "virStoragePoolGetInfo", "", conn(s)));
 
     result = rb_class_new_instance(0, NULL, c_storage_pool_info);
     rb_iv_set(result, "@state", INT2FIX(info.state));
@@ -1221,7 +1271,7 @@ VALUE libvirt_pool_autostart(VALUE s){
     int r, autostart;
 
     r = virStoragePoolGetAutostart(pool_get(s), &autostart);
-    _E(r < 0, conn(s), "virStoragePoolGetAutostart");
+    _E(r < 0, create_error(e_RetrieveError, "virStoragePoolGetAutostart", "", conn(s)));
 
     return autostart ? Qtrue : Qfalse;
 }
@@ -1239,7 +1289,7 @@ VALUE libvirt_pool_autostart_set(VALUE s
  */
 VALUE libvirt_pool_num_of_volumes(VALUE s) {
     int n = virStoragePoolNumOfVolumes(pool_get(s));
-    _E(n < 0, conn(s), "virStoragePoolNumOfVolumes");
+    _E(n < 0, create_error(e_RetrieveError, "virStoragePoolNumOfVolumes", "", conn(s)));
 
     return INT2FIX(n);
 }
@@ -1254,13 +1304,13 @@ VALUE libvirt_pool_list_volumes(VALUE s)
     VALUE result;
 
     num = virStoragePoolNumOfVolumes(pool);
-    _E(num < 0, conn(s), "virStoragePoolNumOfVolumes");
+    _E(num < 0, create_error(e_RetrieveError, "virStoragePoolNumOfVolumes", "", conn(s)));
 
     names = ALLOC_N(char *, num);
     r = virStoragePoolListVolumes(pool, names, num);
     if (r < 0) {
         free(names);
-        _E(r < 0, conn(s), "virStoragePoolListVolumes");
+        _E(r < 0, create_error(e_RetrieveError, "virStoragePoolListVolumes", "", conn(s)));
     }
 
     result = rb_ary_new2(num);
@@ -1285,7 +1335,7 @@ VALUE libvirt_pool_lookup_vol_by_name(VA
     virStorageVolPtr vol;
 
     vol = virStorageVolLookupByName(pool_get(p), StringValueCStr(name));
-    _E(vol == NULL, conn(p), "virStorageVolLookupByName");
+    _E(vol == NULL, create_error(e_RetrieveError, "virStorageVolLookupByName", "", conn(p)));
 
     return vol_new(vol, conn_attr(p));
 }
@@ -1298,7 +1348,7 @@ VALUE libvirt_pool_lookup_vol_by_key(VAL
 
     // FIXME: Why does this take a connection, not a pool ?
     vol = virStorageVolLookupByKey(conn(p), StringValueCStr(key));
-    _E(vol == NULL, conn(p), "virStorageVolLookupByKey");
+    _E(vol == NULL, create_error(e_RetrieveError, "virStorageVolLookupByKey", "", conn(p)));
 
     return vol_new(vol, conn_attr(p));
 }
@@ -1311,7 +1361,7 @@ VALUE libvirt_pool_lookup_vol_by_path(VA
 
     // FIXME: Why does this take a connection, not a pool ?
     vol = virStorageVolLookupByPath(conn(p), StringValueCStr(path));
-    _E(vol == NULL, conn(p), "virStorageVolLookupByPath");
+    _E(vol == NULL, create_error(e_RetrieveError, "virStorageVolLookupByPath", "", conn(p)));
 
     return vol_new(vol, conn_attr(p));
 }
@@ -1343,7 +1393,7 @@ VALUE libvirt_vol_create_xml(VALUE p, VA
     xmlDesc = StringValueCStr(xml);
 
     vol = virStorageVolCreateXML(pool_get(p), xmlDesc, NUM2UINT(flags));
-    _E(vol == NULL, c, "virNetworkCreateXML");
+    _E(vol == NULL, create_error(e_OperationError, "virNetworkCreateXML", "", c));
 
     return vol_new(vol, conn_attr(p));
 }
@@ -1365,7 +1415,7 @@ VALUE libvirt_vol_info(VALUE v) {
     VALUE result;
 
     r = virStorageVolGetInfo(vol_get(v), &info);
-    _E(r < 0, conn(v), "virStorageVolGetInfo");
+    _E(r < 0, create_error(e_RetrieveError, "virStorageVolGetInfo", "", conn(v)));
 
     result = rb_class_new_instance(0, NULL, c_storage_vol_info);
     rb_iv_set(result, "@type", INT2NUM(info.type));
@@ -1486,8 +1536,27 @@ void Init__libvirt() {
     int r;
 
     m_libvirt = rb_define_module("Libvirt");
+
     c_libvirt_version = rb_define_class_under(m_libvirt, "Version",
                                               rb_cObject);
+
+    /*
+     * Libvirt Errors
+     */
+    e_Error =           rb_define_class_under(m_libvirt, "Error", 
+                                               rb_eStandardError);
+    e_ConnectionError = rb_define_class_under(m_libvirt, "ConnectionError", 
+                                                e_Error);
+    e_DefinitionError = rb_define_class_under(m_libvirt, "DefinitionError", 
+                                                e_Error);
+    e_RetrieveError =   rb_define_class_under(m_libvirt, "RetrieveError", 
+                                                e_Error);
+    e_OperationError =  rb_define_class_under(m_libvirt, "OperationError", 
+                                                e_Error);
+
+    // create 'method_name' and 'vir_connect_ptr' attributes on e_Error class
+    rb_define_attr(e_Error, "method_name", 1, 1);
+    rb_define_attr(e_Error, "vir_connect_ptr", 1, 1);
 
     /*
      * Class Libvirt::Connect
@@ -1616,6 +1685,7 @@ void Init__libvirt() {
     rb_define_method(c_domain, "memory=", libvirt_dom_memory_set, 1);
     rb_define_method(c_domain, "max_vcpus", libvirt_dom_max_vcpus, 0);
     rb_define_method(c_domain, "vcpus=", libvirt_dom_vcpus_set, 1);
+    rb_define_method(c_domain, "pin_vcpu", libvirt_dom_pin_vcpu, 2);
     rb_define_method(c_domain, "xml_desc", libvirt_dom_xml_desc, 0);
     rb_define_method(c_domain, "undefine", libvirt_dom_undefine, 0);
     rb_define_method(c_domain, "create", libvirt_dom_create, 0);

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