diff --git a/ext/em.cpp b/ext/em.cpp index 5fe9e4298..2a578745f 100644 --- a/ext/em.cpp +++ b/ext/em.cpp @@ -1021,17 +1021,17 @@ const unsigned long EventMachine_t::ConnectToServer (const char *bind_addr, int */ if (!server || !*server || !port) - return NULL; + throw std::runtime_error ("invalid server or port"); int family, bind_size; struct sockaddr bind_as, *bind_as_ptr = name2address (server, port, &family, &bind_size); if (!bind_as_ptr) - return NULL; + throw std::runtime_error ("unable to resolve server address"); bind_as = *bind_as_ptr; // copy because name2address points to a static int sd = socket (family, SOCK_STREAM, 0); if (sd == INVALID_SOCKET) - return NULL; + throw std::runtime_error ("unable to create new socket"); /* sockaddr_in pin; @@ -1065,7 +1065,7 @@ const unsigned long EventMachine_t::ConnectToServer (const char *bind_addr, int // Set the new socket nonblocking. if (!SetSocketNonblocking (sd)) { closesocket (sd); - return NULL; + throw std::runtime_error ("unable to set socket as non-blocking"); } // Disable slow-start (Nagle algorithm). int one = 1; @@ -1078,7 +1078,7 @@ const unsigned long EventMachine_t::ConnectToServer (const char *bind_addr, int struct sockaddr *bind_to = name2address (bind_addr, bind_port, &bind_to_family, &bind_to_size); if (!bind_to) { closesocket (sd); - throw std::runtime_error ("bad bind address"); + throw std::runtime_error ("invalid bind address"); } if (bind (sd, bind_to, bind_to_size) < 0) { closesocket (sd); diff --git a/ext/rubymain.cpp b/ext/rubymain.cpp index 2ac9363df..ebfdaf144 100644 --- a/ext/rubymain.cpp +++ b/ext/rubymain.cpp @@ -32,6 +32,7 @@ Statics static VALUE EmModule; static VALUE EmConnection; +static VALUE EM_eConnectionError; static VALUE EM_eUnknownTimerFired; static VALUE EM_eConnectionNotBound; static VALUE EM_eUnsupported; @@ -458,10 +459,14 @@ static VALUE t_connect_server (VALUE self, VALUE server, VALUE port) // Specifically, if the value of port comes in as a string rather than an integer, // NUM2INT will throw a type error, but FIX2INT will generate garbage. - const unsigned long f = evma_connect_to_server (NULL, 0, StringValuePtr(server), NUM2INT(port)); - if (!f) - rb_raise (rb_eRuntimeError, "no connection"); - return ULONG2NUM (f); + try { + const unsigned long f = evma_connect_to_server (NULL, 0, StringValuePtr(server), NUM2INT(port)); + if (!f) + rb_raise (EM_eConnectionError, "no connection"); + return ULONG2NUM (f); + } catch (std::runtime_error e) { + rb_raise (EM_eConnectionError, e.what()); + } } /********************* @@ -474,15 +479,14 @@ static VALUE t_bind_connect_server (VALUE self, VALUE bind_addr, VALUE bind_port // Specifically, if the value of port comes in as a string rather than an integer, // NUM2INT will throw a type error, but FIX2INT will generate garbage. - long f; try { - f = evma_connect_to_server (StringValuePtr(bind_addr), NUM2INT(bind_port), StringValuePtr(server), NUM2INT(port)); + const unsigned long f = evma_connect_to_server (StringValuePtr(bind_addr), NUM2INT(bind_port), StringValuePtr(server), NUM2INT(port)); if (!f) - rb_raise (rb_eRuntimeError, "no connection"); + rb_raise (EM_eConnectionError, "no connection"); + return ULONG2NUM (f); } catch (std::runtime_error e) { - rb_sys_fail(e.what()); + rb_raise (EM_eConnectionError, e.what()); } - return ULONG2NUM (f); } /********************* @@ -1048,7 +1052,8 @@ extern "C" void Init_rubyeventmachine() EmModule = rb_define_module ("EventMachine"); EmConnection = rb_define_class_under (EmModule, "Connection", rb_cObject); - rb_define_class_under (EmModule, "NoHandlerForAcceptedConnection", rb_eException); + rb_define_class_under (EmModule, "NoHandlerForAcceptedConnection", rb_eRuntimeError); + EM_eConnectionError = rb_define_class_under (EmModule, "ConnectionError", rb_eRuntimeError); EM_eConnectionNotBound = rb_define_class_under (EmModule, "ConnectionNotBound", rb_eRuntimeError); EM_eUnknownTimerFired = rb_define_class_under (EmModule, "UnknownTimerFired", rb_eRuntimeError); EM_eUnsupported = rb_define_class_under (EmModule, "Unsupported", rb_eRuntimeError);