Log exceptions thrown by MQTT message callbacks (#19977)

* Log exceptions thrown by MQTT message callbacks

* Fix tests

* Correct method for skipping wrapper in traceback

* Lint

* Simplify traceback print

* Add test

* Move wrapper to common helper function

* Typing

* Lint
This commit is contained in:
emontnemery authored and Paulus Schoutsen committed 2019-01-16 13:50:21 -08:00
1 parent 1d86905d5b
commit 368682647d
4 files changed
+66 -4

No files matched your search

+17
View File
@@ -297,6 +297,23 @@ class TestMQTTCallbacks(unittest.TestCase):
"b'\\x9a' on test-topic with encoding utf-8" in \
test_handle.output[0]
def test_message_callback_exception_gets_logged(self):
"""Test exception raised by message handler."""
@callback
def bad_handler(*args):
"""Record calls."""
raise Exception('This is a bad message callback')
mqtt.subscribe(self.hass, 'test-topic', bad_handler)
with self.assertLogs(level='WARNING') as test_handle:
fire_mqtt_message(self.hass, 'test-topic', 'test')
self.hass.block_till_done()
assert \
"Exception in bad_handler when handling msg on 'test-topic':" \
" 'test'" in \
test_handle.output[0]
def test_all_subscriptions_run_when_decode_fails(self):
"""Test all other subscriptions still run when decode fails for one."""
mqtt.subscribe(self.hass, 'test-topic', self.record_calls,