From cd0f9acb4b932557a91387b7d804cb94ac4dcbb3 Mon Sep 17 00:00:00 2001 From: Marnik Bercx Date: Tue, 18 Jun 2024 13:27:07 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20`RabbitmqBroker`:=20catch=20`Con?= =?UTF-8?q?nectionError`=20for=20`=5F=5Fstr=5F=5F`=20(#6473)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The current implementation of the `RabbitmqBroker.__str__()` method always prints both the version and the URL of the RabbitMQ server. However, the `get_rabbitmq_version()` method fails with a `ConnectionError` in case the RabbitMQ broker is not able to connect to the server. This issue would bubble up into the `verdi status` command, since this prints the string representation of the `RabbitmqBroker` in the message that reports the connection failure. At this point the `ConnectionError` is no longer caught, and hence the user is exposed to the full traceback. Here we adapt the `RabbitmqBroker.__str__()` method to catch the `ConnectionError` and return the URL with the message that the connection failed. --- src/aiida/brokers/rabbitmq/broker.py | 5 ++++- tests/brokers/test_rabbitmq.py | 13 +++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/aiida/brokers/rabbitmq/broker.py b/src/aiida/brokers/rabbitmq/broker.py index dab19e28b7..5321f6d400 100644 --- a/src/aiida/brokers/rabbitmq/broker.py +++ b/src/aiida/brokers/rabbitmq/broker.py @@ -34,7 +34,10 @@ def __init__(self, profile: Profile) -> None: self._prefix = f'aiida-{self._profile.uuid}' def __str__(self): - return f'RabbitMQ v{self.get_rabbitmq_version()} @ {self.get_url()}' + try: + return f'RabbitMQ v{self.get_rabbitmq_version()} @ {self.get_url()}' + except ConnectionError: + return f'RabbitMQ @ {self.get_url()} ' def close(self): """Close the broker.""" diff --git a/tests/brokers/test_rabbitmq.py b/tests/brokers/test_rabbitmq.py index fc27a3eaf6..00ee662338 100644 --- a/tests/brokers/test_rabbitmq.py +++ b/tests/brokers/test_rabbitmq.py @@ -22,6 +22,19 @@ pytestmark = pytest.mark.requires_rmq +def test_str_method(monkeypatch, manager): + """Test the `__str__` method of the `RabbitmqBroker`.""" + + def raise_connection_error(): + raise ConnectionError + + broker = manager.get_broker() + assert 'RabbitMQ v' in str(broker) + + monkeypatch.setattr(broker, 'get_communicator', raise_connection_error) + assert 'RabbitMQ @' in str(broker) + + @pytest.mark.parametrize( ('version', 'supported'), (