canonical / hardware-observer-operator

A charm to setup prometheus exporter for IPMI, RedFish and RAID devices from different vendors.
Apache License 2.0
7 stars 14 forks source link

fix: Raise custom exception instead of ErrorStatus #191

Closed dashmage closed 4 months ago

dashmage commented 4 months ago

Raise custom exception ExporterError instead of failing with ops.model.ModelError when restart_exporter fails.

Couple of points to note:

The new error after this patch would look something like

unit-hardware-observer-5: 12:16:57 ERROR unit.hardware-observer/5.juju-log Failed to restart the exporter.
unit-hardware-observer-5: 12:16:57 ERROR unit.hardware-observer/5.juju-log Uncaught exception while in cha
Traceback (most recent call last):
  File "/var/lib/juju/agents/unit-hardware-observer-5/charm/./src/charm.py", line 344, in <module>
    ops.main(HardwareObserverCharm)  # type: ignore
  File "/var/lib/juju/agents/unit-hardware-observer-5/charm/venv/ops/main.py", line 451, in __call__
    return main(charm_class, use_juju_for_storage=use_juju_for_storage)
  File "/var/lib/juju/agents/unit-hardware-observer-5/charm/venv/ops/main.py", line 436, in main
    _emit_charm_event(charm, dispatcher.event_name)
  File "/var/lib/juju/agents/unit-hardware-observer-5/charm/venv/ops/main.py", line 144, in _emit_charm_event
    event_to_emit.emit(*args, **kwargs)
  File "/var/lib/juju/agents/unit-hardware-observer-5/charm/venv/ops/framework.py", line 351, in emit
    framework._emit(event)
  File "/var/lib/juju/agents/unit-hardware-observer-5/charm/venv/ops/framework.py", line 853, in _emit
    self._reemit(event_path)
  File "/var/lib/juju/agents/unit-hardware-observer-5/charm/venv/ops/framework.py", line 942, in _reemit
    custom_handler(event)
  File "/var/lib/juju/agents/unit-hardware-observer-5/charm/./src/charm.py", line 224, in _on_config_changed
    self._on_update_status(event)
  File "/var/lib/juju/agents/unit-hardware-observer-5/charm/./src/charm.py", line 148, in _on_update_status
    raise ExporterError(restart_msg)
service.ExporterError: Exporter crashed unexpectedly, please refer to systemd logs...
unit-hardware-observer-5: 12:16:58 ERROR juju.worker.uniter.operation hook "config-changed" (via hook dispatching script: dispatch) failed: exit status 1

I would also like to hear about any other ways to handle this sort of issue where BlockedStatus wouldn't work since the user can't fix the exporter crash.

dashmage commented 4 months ago

Didn't realize this change breaks a couple of unit tests. Will push the required changes to fix the unit tests soon.

Edit: unit test changes are done