Commits

Michael Granger committed 97180d0

Use ZMQ::Error instead of RuntimeErrors to make exception-handling around sockets more predictable.

  • Participants
  • Parent commits 754d62d

Comments (0)

Files changed (1)

     } while(0)
 
 VALUE socket_type;
+VALUE exception_type;
 
 /*
  * Document-class: ZMQ
     assert (!DATA_PTR (self_));
     void *ctx = zmq_init (NIL_P (io_threads) ? 1 : NUM2INT (io_threads));
     if (!ctx) {
-        rb_raise (rb_eRuntimeError, "%s", zmq_strerror (zmq_errno ()));
+        rb_raise (exception_type, "%s", zmq_strerror (zmq_errno ()));
         return Qnil;
     }
 
         rc = zmq_poll (ps.items, ps.nitems, arg->timeout_usec);
     
     if (rc == -1) {
-        rb_raise(rb_eRuntimeError, "%s", zmq_strerror (zmq_errno ()));
+        rb_raise(exception_type, "%s", zmq_strerror (zmq_errno ()));
         return Qnil;
     }
     else if (rc == 0)
     Data_Get_Struct (self_, void, c);
     void * s = zmq_socket (c, NUM2INT (type_));
     if (!s) {
-        rb_raise (rb_eRuntimeError, "%s", zmq_strerror (zmq_errno ()));
+        rb_raise (exception_type, "%s", zmq_strerror (zmq_errno ()));
         return Qnil;
     }
 
                                  &optvalsize);
 
             if (rc != 0) {
-              rb_raise (rb_eRuntimeError, "%s", zmq_strerror (zmq_errno ()));
+              rb_raise (exception_type, "%s", zmq_strerror (zmq_errno ()));
               return Qnil;
             }
 
                                  &optvalsize);
 
             if (rc != 0) {
-              rb_raise (rb_eRuntimeError, "%s", zmq_strerror (zmq_errno ()));
+              rb_raise (exception_type, "%s", zmq_strerror (zmq_errno ()));
               return Qnil;
             }
 
                                  &optvalsize);
 
             if (rc != 0) {
-              rb_raise (rb_eRuntimeError, "%s", zmq_strerror (zmq_errno ()));
+              rb_raise (exception_type, "%s", zmq_strerror (zmq_errno ()));
               return Qnil;
             }
 
                                  &optvalsize);
 
             if (rc != 0) {
-              rb_raise (rb_eRuntimeError, "%s", zmq_strerror (zmq_errno ()));
+              rb_raise (exception_type, "%s", zmq_strerror (zmq_errno ()));
               return Qnil;
             }
 
                                  &optvalsize);
 
             if (rc != 0) {
-              rb_raise (rb_eRuntimeError, "%s", zmq_strerror (zmq_errno ()));
+              rb_raise (exception_type, "%s", zmq_strerror (zmq_errno ()));
               return Qnil;
             }
 
         }
         break;
     default:
-        rb_raise (rb_eRuntimeError, "%s", zmq_strerror (EINVAL));
+        rb_raise (exception_type, "%s", zmq_strerror (EINVAL));
         return Qnil;
     }
   
         break;
 
     default:
-        rb_raise (rb_eRuntimeError, "%s", zmq_strerror (EINVAL));
+        rb_raise (exception_type, "%s", zmq_strerror (EINVAL));
         return Qnil;
     }
 
     if (rc != 0) {
-        rb_raise (rb_eRuntimeError, "%s", zmq_strerror (zmq_errno ()));
+        rb_raise (exception_type, "%s", zmq_strerror (zmq_errno ()));
         return Qnil;
     }
 
 
     int rc = zmq_bind (s, rb_string_value_cstr (&addr_));
     if (rc != 0) {
-        rb_raise (rb_eRuntimeError, "%s", zmq_strerror (zmq_errno ()));
+        rb_raise (exception_type, "%s", zmq_strerror (zmq_errno ()));
         return Qnil;
     }
 
 
     int rc = zmq_connect (s, rb_string_value_cstr (&addr_));
     if (rc != 0) {
-        rb_raise (rb_eRuntimeError, "%s", zmq_strerror (zmq_errno ()));
+        rb_raise (exception_type, "%s", zmq_strerror (zmq_errno ()));
         return Qnil;
     }
 
     zmq_msg_t msg;
     int rc = zmq_msg_init_size (&msg, RSTRING_LEN (msg_));
     if (rc != 0) {
-        rb_raise (rb_eRuntimeError, "%s", zmq_strerror (zmq_errno ()));
+        rb_raise (exception_type, "%s", zmq_strerror (zmq_errno ()));
         return Qnil;
     }
     memcpy (zmq_msg_data (&msg), RSTRING_PTR (msg_), RSTRING_LEN (msg_));
     }
 
     if (rc != 0) {
-        rb_raise (rb_eRuntimeError, "%s", zmq_strerror (zmq_errno ()));
+        rb_raise (exception_type, "%s", zmq_strerror (zmq_errno ()));
         rc = zmq_msg_close (&msg);
         assert (rc == 0);
         return Qnil;
     }
 
     if (rc != 0) {
-        rb_raise (rb_eRuntimeError, "%s", zmq_strerror (zmq_errno ()));
+        rb_raise (exception_type, "%s", zmq_strerror (zmq_errno ()));
         rc = zmq_msg_close (&msg);
         assert (rc == 0);
         return Qnil;
     if (s != NULL) {
         int rc = zmq_close (s);
         if (rc != 0) {
-            rb_raise (rb_eRuntimeError, "%s", zmq_strerror (zmq_errno ()));
+            rb_raise (exception_type, "%s", zmq_strerror (zmq_errno ()));
             return Qnil;
         }
 
     rb_define_singleton_method (zmq_module, "version", module_version, 0);
     rb_define_singleton_method (zmq_module, "select", module_select, -1);
 
+	exception_type = rb_define_class_under (zmq_module, "Error", rb_eRuntimeError );
+
     VALUE context_type = rb_define_class_under (zmq_module, "Context",
         rb_cObject);
     rb_define_alloc_func (context_type, context_alloc);