Catch UnicodeDecodeError Error (#4007)

* Catch UnicodeDecodeError Error

Error for #3933

* Forgot (exc)

* catch...

* Tests by @lwis

* Docstring

* Create open
This commit is contained in:
Johann Kellerman 2016-10-24 03:55:06 +02:00 committed by Paulus Schoutsen
parent c32f47aea6
commit f0a38dded6
2 changed files with 14 additions and 0 deletions

View File

@ -43,6 +43,9 @@ def load_yaml(fname: str) -> Union[List, Dict]:
except yaml.YAMLError as exc:
_LOGGER.error(exc)
raise HomeAssistantError(exc)
except UnicodeDecodeError as exc:
_LOGGER.error('Unable to read file %s: %s', fname, exc)
raise HomeAssistantError(exc)
def clear_secret_cache() -> None:

View File

@ -92,6 +92,7 @@ class TestYaml(unittest.TestCase):
mock_walk.return_value = [
['/tmp', ['tmp2'], ['zero.yaml']],
['/tmp/tmp2', [], ['one.yaml', 'two.yaml']],
['/tmp/ignore', [], ['.ignore.yaml']]
]
with patch_yaml_files({
@ -123,6 +124,7 @@ class TestYaml(unittest.TestCase):
mock_walk.return_value = [
['/tmp', ['tmp2'], ['first.yaml']],
['/tmp/tmp2', [], ['second.yaml', 'third.yaml']],
['/tmp/ignore', [], ['.ignore.yaml']]
]
with patch_yaml_files({
@ -155,6 +157,7 @@ class TestYaml(unittest.TestCase):
mock_walk.return_value = [
['/tmp', ['tmp2'], ['first.yaml']],
['/tmp/tmp2', [], ['second.yaml', 'third.yaml']],
['/tmp/ignore', [], ['.ignore.yaml']]
]
with patch_yaml_files({
@ -191,6 +194,7 @@ class TestYaml(unittest.TestCase):
mock_walk.return_value = [
['/tmp', ['tmp2'], ['first.yaml']],
['/tmp/tmp2', [], ['second.yaml', 'third.yaml']],
['/tmp/ignore', [], ['.ignore.yaml']]
]
with patch_yaml_files({
@ -208,6 +212,13 @@ class TestYaml(unittest.TestCase):
"key4": "four"
}
@patch('homeassistant.util.yaml.open', create=True)
def test_load_yaml_encoding_error(self, mock_open):
"""Test raising a UnicodeDecodeError."""
mock_open.side_effect = UnicodeDecodeError('', b'', 1, 0, '')
self.assertRaises(HomeAssistantError, yaml.load_yaml, 'test')
FILES = {}