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,
+4 -2
View File
@@ -1,4 +1,6 @@
"""The tests for the MQTT subscription component."""
from unittest import mock
from homeassistant.core import callback
from homeassistant.components.mqtt.subscription import (
async_subscribe_topics, async_unsubscribe_topics)
@@ -135,7 +137,7 @@ async def test_qos_encoding_default(hass, mqtt_mock, caplog):
{'test_topic1': {'topic': 'test-topic1',
'msg_callback': msg_callback}})
mock_mqtt.async_subscribe.assert_called_once_with(
'test-topic1', msg_callback, 0, 'utf-8')
'test-topic1', mock.ANY, 0, 'utf-8')
async def test_qos_encoding_custom(hass, mqtt_mock, caplog):
@@ -155,7 +157,7 @@ async def test_qos_encoding_custom(hass, mqtt_mock, caplog):
'qos': 1,
'encoding': 'utf-16'}})
mock_mqtt.async_subscribe.assert_called_once_with(
'test-topic1', msg_callback, 1, 'utf-16')
'test-topic1', mock.ANY, 1, 'utf-16')
async def test_no_change(hass, mqtt_mock, caplog):