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

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



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!


20c20
<  * Author: David Lutterkort <dlutter redhat com>
---
>  * Authors: David Lutterkort <dlutter redhat com>, Mohammed Morsi <mmorsi redhat com>
44a45,52
> // 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
> 
> 
75a84
> 
113a123,132
> /* 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;
> };
> 
173,174c192,193
< #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)
176c195
< NORETURN(static void vir_error(virConnectPtr conn, const char *fn));
---
> NORETURN(static void vir_error(VALUE exception));
178,179c197,199
< 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);
200c220
<         _E(result < 0, conn, "virConnectNumOf" # objs);               \
---
>         _E(result < 0, create_error(e_RetrieveError, "virConnectNumOf" # objs, "", conn));                \
219c239
<         _E(num < 0, conn, "virConnectNumOf" # objs);                  \
---
>         _E(num < 0, create_error(e_RetrieveError, "virConnectNumOf" # objs, "", conn));   \
225c245
<             _E(r < 0, conn, "virConnectList" # objs);                   \
---
>             _E(r < 0, create_error(e_RetrieveError, "virConnectList" # objs, "", conn));  \
245c265
<         _E(_r_##func < 0, conn, #func);                               \
---
>         _E(_r_##func < 0, create_error(e_Error, #func, "", conn));                        \
259c279
<         _E(str == NULL, conn, # func);                                  \
---
>         _E(str == NULL, create_error(e_Error, # func, "", conn));                         \
339c359
<     _E(!ptr, NULL, "virConnectOpenReadOnly");
---
>     _E(!ptr, create_error(e_ConnectionError, "virConnectOpenReadOnly", "", NULL));
399c419
<     _E(r < 0, conn, "virConnectGetVersion");
---
>     _E(r < 0, create_error(e_RetrieveError, "virConnectGetVersion", "", conn));
432c452
<     _E(result < 0, conn, "virConnectGetMaxVcpus");
---
>     _E(result < 0, create_error(e_RetrieveError, "virConnectGetMaxVcpus", "", conn));
448c468
<     _E(r < 0, conn, "virNodeGetInfo");
---
>     _E(r < 0, create_error(e_RetrieveError, "virNodeGetInfo", "", conn));
489c509
<     _E(num < 0, conn, "virConnectNumOfDomains");
---
>     _E(num < 0, create_error(e_RetrieveError, "virConnectNumOfDomains", "", conn));
495c515
<         _E(r < 0, conn, "virConnectListDomains");
---
>         _E(r < 0, create_error(e_RetrieveError, "virConnectListDomains", "", conn));
663c683
<     _E(r < 0, conn(s), "virDomainGetInfo");
---
>     _E(r < 0, create_error(e_RetrieveError, "virDomainGetInfo", "", conn(s)));
691c711
<     _E(id < 0, conn(s), "virDomainGetID");
---
>     _E(id < 0, create_error(e_RetrieveError, "virDomainGetID", "", conn(s)));
705c725
<     _E(r < 0, conn(s), "virDomainGetUUIDString");
---
>     _E(r < 0, create_error(e_RetrieveError, "virDomainGetUUIDString", "", conn(s)));
726c746
<     _E(max_memory == 0, conn(s), "virDomainGetMaxMemory");
---
>     _E(max_memory == 0, create_error(e_RetrieveError, "virDomainGetMaxMemory", "", conn(s)));
739c759
<     _E(r < 0, conn(s), "virDomainSetMaxMemory");
---
>     _E(r < 0, create_error(e_DefinitionError, "virDomainSetMaxMemory", "", conn(s)));
752c772
<     _E(r < 0, conn(s), "virDomainSetMemory");
---
>     _E(r < 0, create_error(e_DefinitionError, "virDomainSetMemory", "", conn(s)));
765c785
<     _E(vcpus < 0, conn(s), "virDomainGetMaxVcpus");
---
>     _E(vcpus < 0, create_error(e_RetrieveError, "virDomainGetMaxVcpus", "", conn(s)));
779c799
<     _E(r < 0, conn(s), "virDomainSetVcpus");
---
>     _E(r < 0, create_error(e_DefinitionError, "virDomainSetVcpus", "", conn(s)));
795c815
<     _E(r < 0, c, "virNodeGetInfo");
---
>     _E(r < 0, create_error(e_RetrieveError, "virNodeGetInfo", "", c));
809c829
<     _E(r < 0, c, "virDomainPinVcpu");
---
>     _E(r < 0, create_error(e_OperationError, "virDomainPinVcpu", "", c));
847c867
<     _E(r < 0, conn(s), "virDomainAutostart");
---
>     _E(r < 0, create_error(e_RetrieveError, "virDomainAutostart", "", conn(s)));
871c891
<     _E(dom == NULL, conn, "virDomainCreateLinux");
---
>     _E(dom == NULL, create_error(e_OperationError, "virDomainCreateLinux", "", conn));
884c904
<     _E(dom == NULL, conn, "virDomainLookupByName");
---
>     _E(dom == NULL, create_error(e_RetrieveError, "virDomainLookupByName", "", conn));
897c917
<     _E(dom == NULL, conn, "virDomainLookupByID");
---
>     _E(dom == NULL, create_error(e_RetrieveError, "virDomainLookupByID", "", conn));
910c930
<     _E(dom == NULL, conn, "virDomainLookupByUUID");
---
>     _E(dom == NULL, create_error(e_RetrieveError, "virDomainLookupByUUID", "", conn));
923c943
<     _E(dom == NULL, conn, "virDomainDefineXML");
---
>     _E(dom == NULL, create_error(e_DefinitionError, "virDomainDefineXML", "", conn));
941c961
<     _E(netw == NULL, conn, "virNetworkLookupByName");
---
>     _E(netw == NULL, create_error(e_RetrieveError, "virNetworkLookupByName", "", conn));
954c974
<     _E(netw == NULL, conn, "virNetworkLookupByUUID");
---
>     _E(netw == NULL, create_error(e_RetrieveError, "virNetworkLookupByUUID", "", conn));
970c990
<     _E(netw == NULL, conn, "virNetworkCreateXML");
---
>     _E(netw == NULL, create_error(e_OperationError, "virNetworkCreateXML", "", conn));
983c1003
<     _E(netw == NULL, conn, "virNetworkDefineXML");
---
>     _E(netw == NULL, create_error(e_DefinitionError, "virNetworkDefineXML", "", conn));
1031c1051
<     _E(r < 0, conn(s), "virNetworkGetUUIDString");
---
>     _E(r < 0, create_error(e_RetrieveError, "virNetworkGetUUIDString", "", conn(s)));
1060c1080
<     _E(r < 0, conn(s), "virNetworkAutostart");
---
>     _E(r < 0, create_error(e_RetrieveError, "virNetworkAutostart", "", conn(s)));
1087c1107
<     _E(pool == NULL, conn, "virStoragePoolLookupByName");
---
>     _E(pool == NULL, create_error(e_RetrieveError, "virStoragePoolLookupByName", "", conn));
1100c1120
<     _E(pool == NULL, conn, "virStoragePoolLookupByUUID");
---
>     _E(pool == NULL, create_error(e_RetrieveError, "virStoragePoolLookupByUUID", "", conn));
1112c1132
<     _E(pool == NULL, conn(v), "virStoragePoolLookupByVolume");
---
>     _E(pool == NULL, create_error(e_RetrieveError, "virStoragePoolLookupByVolume", "", conn(v)));
1128c1148
<     _E(pool == NULL, conn, "virStoragePoolCreateXML");
---
>     _E(pool == NULL, create_error(e_OperationError, "virStoragePoolCreateXML", "", conn));
1141c1161
<     _E(pool == NULL, conn, "virStoragePoolDefineXML");
---
>     _E(pool == NULL, create_error(e_DefinitionError, "virStoragePoolDefineXML", "", conn));
1201c1221
<     _E(name == NULL, conn(s), "virStoragePoolGetName");
---
>     _E(name == NULL, create_error(e_RetrieveError, "virStoragePoolGetName", "", conn(s)));
1214c1234
<     _E(r < 0, conn(s), "virStoragePoolGetUUIDString");
---
>     _E(r < 0, create_error(e_RetrieveError, "virStoragePoolGetUUIDString", "", conn(s)));
1228c1248
<     _E(r < 0, conn(s), "virStoragePoolGetInfo");
---
>     _E(r < 0, create_error(e_RetrieveError, "virStoragePoolGetInfo", "", conn(s)));
1254c1274
<     _E(r < 0, conn(s), "virStoragePoolGetAutostart");
---
>     _E(r < 0, create_error(e_RetrieveError, "virStoragePoolGetAutostart", "", conn(s)));
1272c1292
<     _E(n < 0, conn(s), "virStoragePoolNumOfVolumes");
---
>     _E(n < 0, create_error(e_RetrieveError, "virStoragePoolNumOfVolumes", "", conn(s)));
1287c1307
<     _E(num < 0, conn(s), "virStoragePoolNumOfVolumes");
---
>     _E(num < 0, create_error(e_RetrieveError, "virStoragePoolNumOfVolumes", "", conn(s)));
1293c1313
<         _E(r < 0, conn(s), "virStoragePoolListVolumes");
---
>         _E(r < 0, create_error(e_RetrieveError, "virStoragePoolListVolumes", "", conn(s)));
1318c1338
<     _E(vol == NULL, conn(p), "virStorageVolLookupByName");
---
>     _E(vol == NULL, create_error(e_RetrieveError, "virStorageVolLookupByName", "", conn(p)));
1331c1351
<     _E(vol == NULL, conn(p), "virStorageVolLookupByKey");
---
>     _E(vol == NULL, create_error(e_RetrieveError, "virStorageVolLookupByKey", "", conn(p)));
1344c1364
<     _E(vol == NULL, conn(p), "virStorageVolLookupByPath");
---
>     _E(vol == NULL, create_error(e_RetrieveError, "virStorageVolLookupByPath", "", conn(p)));
1376c1396
<     _E(vol == NULL, c, "virNetworkCreateXML");
---
>     _E(vol == NULL, create_error(e_OperationError, "virNetworkCreateXML", "", c));
1398c1418
<     _E(r < 0, conn(v), "virStorageVolGetInfo");
---
>     _E(r < 0, create_error(e_RetrieveError, "virStorageVolGetInfo", "", conn(v)));
1518a1539
> 
1522a1544,1561
>      * 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);
> 
>     /*

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