From 7080f9f638b78423ed27a2d4c36e082b3a3d031f Mon Sep 17 00:00:00 2001 From: Simon Li Date: Wed, 8 Apr 2020 15:09:58 +0100 Subject: [PATCH 1/2] BlitzGateway.connect: add raiseOnError --- src/omero/gateway/__init__.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/omero/gateway/__init__.py b/src/omero/gateway/__init__.py index 9602f1855..b42472015 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): @@ -2154,12 +2152,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 """ @@ -2168,6 +2167,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: @@ -2193,6 +2194,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 @@ -2235,7 +2238,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(): " + @@ -2253,7 +2256,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 @@ -2276,10 +2279,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!") From d68e639b52d3b583d12c65164f3edcd0b813a4bb Mon Sep 17 00:00:00 2001 From: Simon Li Date: Wed, 8 Apr 2020 15:10:11 +0100 Subject: [PATCH 2/2] BlitzGateway.connect: test raiseOnError --- test/unit/test_gateway.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) 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)