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 1f89e6ddba
commit 5df8477536
2 changed files with 10 additions and 4 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

@ -95,7 +95,6 @@ class TestYaml(unittest.TestCase):
mock_walk.return_value = [
['/tmp', ['tmp2', '.ignore', 'ignore'], ['zero.yaml']],
['/tmp/tmp2', [], ['one.yaml', 'two.yaml']],
['/tmp/.ignore', [], []],
['/tmp/ignore', [], ['.ignore.yaml']]
]
@ -136,7 +135,6 @@ class TestYaml(unittest.TestCase):
mock_walk.return_value = [
['/tmp', ['tmp2', '.ignore', 'ignore'], ['first.yaml']],
['/tmp/tmp2', [], ['second.yaml', 'third.yaml']],
['/tmp/.ignore', [], []],
['/tmp/ignore', [], ['.ignore.yaml']]
]
@ -175,7 +173,6 @@ class TestYaml(unittest.TestCase):
mock_walk.return_value = [
['/tmp', ['tmp2', '.ignore', 'ignore'], ['first.yaml']],
['/tmp/tmp2', [], ['second.yaml', 'third.yaml']],
['/tmp/.ignore', [], []],
['/tmp/ignore', [], ['.ignore.yaml']]
]
@ -218,7 +215,6 @@ class TestYaml(unittest.TestCase):
mock_walk.return_value = [
['/tmp', ['tmp2', '.ignore', 'ignore'], ['first.yaml']],
['/tmp/tmp2', [], ['second.yaml', 'third.yaml']],
['/tmp/.ignore', [], []],
['/tmp/ignore', [], ['.ignore.yaml']]
]
@ -241,6 +237,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 = {}