Skip to content

Commit

Permalink
🐛 RabbitmqBroker: catch ConnectionError for __str__ (#6473)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mbercx authored Jun 18, 2024
1 parent 202a3ec commit cd0f9ac
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/aiida/brokers/rabbitmq/broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()} <Connection failed>'

def close(self):
"""Close the broker."""
Expand Down
13 changes: 13 additions & 0 deletions tests/brokers/test_rabbitmq.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
(
Expand Down

0 comments on commit cd0f9ac

Please sign in to comment.