Anonymous avatar Anonymous committed 566d21c

Never raise SystemExit !!!

Comments (0)

Files changed (2)

     """Couldn't connect to the STOMP server."""
 
 
+class ConnectionTimeoutError(socket.timeout):
+    """Timed-out while establishing connection to the STOMP server."""
+
+
 class Stomp(object):
     """STOMP Client.
 
     :param port: The port to use. (default ``61613``)
 
     """
+    ConnectionError = ConnectionError
+    ConnectionTimeoutError = ConnectionTimeoutError
+    NotConnectedError = NotConnectedError
+
+
     def __init__(self, hostname, port=61613):
         self.host = hostname
         self.port = port
             self.sock.connect((self.host, self.port))
             self.frame.connect(self.sock)
             self.connected = True
-        except (socket.error, socket.timeout), err:
-            print "Cannot connect to %s on port %d" %(self.host,self.port)
-            print "Caught error: %s" % err
-            raise SystemExit
+        except socket.error, exc:
+            raise self.ConnectionError(*exc.args)
+        except socket.timeout, exc:
+            raise self.ConnectionTimeoutError(*exc.args)
 
     def disconnect(self, conf=None):
         """Disconnect from the server."""
     
     def _is_connected(self):
         if not self.connected:
-            raise NotConnectedError("Not connected to STOMP server.")
+            raise self.NotConnectedError("Not connected to STOMP server.")
 
     def _get_subscribed(self):
         return self._subscribed

tests/test_stomp.py

 class WhenSocketCantConnect(TestCase):
     def should_fail_connect(self):
         self.stomp = Stomp('localhost',99999)
-        self.failUnlessRaises(SystemExit,self.stomp.connect)
+        self.failUnlessRaises(self.stomp.ConnectionError,
+                self.stomp.connect)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.