diff --git a/src/omero/gateway/__init__.py b/src/omero/gateway/__init__.py index 47e186413..ae23448d7 100644 --- a/src/omero/gateway/__init__.py +++ b/src/omero/gateway/__init__.py @@ -1616,9 +1616,7 @@ def __enter__(self): print list(conn.getObjects('Project')) """ if not self._connected: - r = self.connect() - if not r: - raise Exception("Connect failed") + self.connect(raiseOnError=True) return self def __exit__(self, *args): @@ -2155,12 +2153,13 @@ def _resetOmeroClient(self): if self.userip is not None: self.c.setIP(self.userip) - def connect(self, sUuid=None): + def connect(self, sUuid=None, raiseOnError=False): """ Creates or retrieves connection for the given sessionUuid. Returns True if connected. :param sUuid: omero_model_SessionI + :param raiseOnError: Boolean :return: Boolean """ @@ -2169,6 +2168,8 @@ def connect(self, sUuid=None): if not self.c: # pragma: no cover self._connected = False logger.debug("Ooops. no self._c") + if raiseOnError: + raise Exception('No self.c') return False try: if self._sessionUuid is None and sUuid: @@ -2194,6 +2195,8 @@ def connect(self, sUuid=None): raise except Exception as x: # pragma: no cover logger.debug("Error: " + str(x)) + if raiseOnError: + raise self._sessionUuid = None if sUuid: return False @@ -2236,7 +2239,7 @@ def connect(self, sUuid=None): self._closeSession() self._sessionUuid = None self._connected = True - return self.connect() + return self.connect(raiseOnError=raiseOnError) else: # pragma: no cover logger.debug( "BlitzGateway.connect().createSession(): " + @@ -2254,7 +2257,7 @@ def connect(self, sUuid=None): "## User not in '%s' group" % self.group) self.group = None self._connected = True - return self.connect() + return self.connect(raiseOnError=raiseOnError) else: raise except Ice.SyscallException: # pragma: no cover @@ -2277,10 +2280,14 @@ def connect(self, sUuid=None): raise except Ice.LocalException as x: # pragma: no cover logger.debug("connect(): " + traceback.format_exc()) + if raiseOnError: + raise self._last_error = x return False except Exception as x: # pragma: no cover logger.debug("connect(): " + traceback.format_exc()) + if raiseOnError: + raise self._last_error = x return False logger.debug(".. connected!") diff --git a/test/unit/test_gateway.py b/test/unit/test_gateway.py index 3f34480ca..4d3d0363e 100644 --- a/test/unit/test_gateway.py +++ b/test/unit/test_gateway.py @@ -360,3 +360,19 @@ def test_simple_marshal_not_tiled(self, wrapped_image): data = wrapped_image.simpleMarshal(xtra={'tiled': True}) self.assert_data(data) assert data['tiled'] is False + + +class TestBlitzGatewayConnect(object): + + def test_connect_default(self): + conn = BlitzGateway( + host='this.host.does.not.exist.example.org', + username='username', passwd='passwd') + assert conn.connect() == False + + def test_connect_raise(self): + conn = BlitzGateway( + host='this.host.does.not.exist.example.org', + username='username', passwd='passwd') + with pytest.raises(Ice.DNSException): + conn.connect(raiseOnError=True)