Fix mqtt callback exception logging (#118138)

* Fix mqtt callback exception logging

* Improve code

* Add test
This commit is contained in:
Jan Bouwhuis
2024-05-25 22:46:33 +02:00
committed by GitHub
parent 569763b7a8
commit 521ed0a220
2 changed files with 43 additions and 3 deletions

View File

@ -835,8 +835,13 @@ class MQTT:
msg: ReceiveMessage,
) -> str:
"""Return a string with the exception message."""
# if msg_callback is a partial we return the name of the first argument
if isinstance(msg_callback, partial):
call_back_name = getattr(msg_callback.args[0], "__name__") # type: ignore[unreachable]
else:
call_back_name = getattr(msg_callback, "__name__")
return (
f"Exception in {msg_callback.__name__} when handling msg on "
f"Exception in {call_back_name} when handling msg on "
f"'{msg.topic}': '{msg.payload}'" # type: ignore[str-bytes-safe]
)