mirror of
https://github.com/home-assistant/core.git
synced 2025-07-13 08:17:08 +00:00
Re-raise annotated_yaml.YAMLException as HomeAssistantError (#147129)
* Re-raise annotated_yaml.YAMLException as HomeAssistantError * Fix comment
This commit is contained in:
parent
77dca49c75
commit
5bc2e271d2
@ -6,7 +6,7 @@ from io import StringIO
|
|||||||
import os
|
import os
|
||||||
from typing import TextIO
|
from typing import TextIO
|
||||||
|
|
||||||
from annotatedyaml import YAMLException, YamlTypeError
|
import annotatedyaml
|
||||||
from annotatedyaml.loader import (
|
from annotatedyaml.loader import (
|
||||||
HAS_C_LOADER,
|
HAS_C_LOADER,
|
||||||
JSON_TYPE,
|
JSON_TYPE,
|
||||||
@ -35,6 +35,10 @@ __all__ = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
class YamlTypeError(HomeAssistantError):
|
||||||
|
"""Raised by load_yaml_dict if top level data is not a dict."""
|
||||||
|
|
||||||
|
|
||||||
def load_yaml(
|
def load_yaml(
|
||||||
fname: str | os.PathLike[str], secrets: Secrets | None = None
|
fname: str | os.PathLike[str], secrets: Secrets | None = None
|
||||||
) -> JSON_TYPE | None:
|
) -> JSON_TYPE | None:
|
||||||
@ -45,7 +49,7 @@ def load_yaml(
|
|||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
return load_annotated_yaml(fname, secrets)
|
return load_annotated_yaml(fname, secrets)
|
||||||
except YAMLException as exc:
|
except annotatedyaml.YAMLException as exc:
|
||||||
raise HomeAssistantError(str(exc)) from exc
|
raise HomeAssistantError(str(exc)) from exc
|
||||||
|
|
||||||
|
|
||||||
@ -59,9 +63,9 @@ def load_yaml_dict(
|
|||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
return load_annotated_yaml_dict(fname, secrets)
|
return load_annotated_yaml_dict(fname, secrets)
|
||||||
except YamlTypeError:
|
except annotatedyaml.YamlTypeError as exc:
|
||||||
raise
|
raise YamlTypeError(str(exc)) from exc
|
||||||
except YAMLException as exc:
|
except annotatedyaml.YAMLException as exc:
|
||||||
raise HomeAssistantError(str(exc)) from exc
|
raise HomeAssistantError(str(exc)) from exc
|
||||||
|
|
||||||
|
|
||||||
@ -71,7 +75,7 @@ def parse_yaml(
|
|||||||
"""Parse YAML with the fastest available loader."""
|
"""Parse YAML with the fastest available loader."""
|
||||||
try:
|
try:
|
||||||
return parse_annotated_yaml(content, secrets)
|
return parse_annotated_yaml(content, secrets)
|
||||||
except YAMLException as exc:
|
except annotatedyaml.YAMLException as exc:
|
||||||
raise HomeAssistantError(str(exc)) from exc
|
raise HomeAssistantError(str(exc)) from exc
|
||||||
|
|
||||||
|
|
||||||
@ -79,5 +83,5 @@ def secret_yaml(loader: LoaderType, node: yaml.nodes.Node) -> JSON_TYPE:
|
|||||||
"""Load secrets and embed it into the configuration YAML."""
|
"""Load secrets and embed it into the configuration YAML."""
|
||||||
try:
|
try:
|
||||||
return annotated_secret_yaml(loader, node)
|
return annotated_secret_yaml(loader, node)
|
||||||
except YAMLException as exc:
|
except annotatedyaml.YAMLException as exc:
|
||||||
raise HomeAssistantError(str(exc)) from exc
|
raise HomeAssistantError(str(exc)) from exc
|
||||||
|
@ -559,6 +559,10 @@ def test_load_yaml_dict(expected_data: Any) -> None:
|
|||||||
@pytest.mark.usefixtures("try_both_loaders", "mock_hass_config_yaml")
|
@pytest.mark.usefixtures("try_both_loaders", "mock_hass_config_yaml")
|
||||||
def test_load_yaml_dict_fail() -> None:
|
def test_load_yaml_dict_fail() -> None:
|
||||||
"""Test item without a key."""
|
"""Test item without a key."""
|
||||||
|
# Make sure we raise a subclass of HomeAssistantError, not
|
||||||
|
# annotated_yaml.YAMLException
|
||||||
|
assert issubclass(yaml_loader.YamlTypeError, HomeAssistantError)
|
||||||
|
|
||||||
with pytest.raises(yaml_loader.YamlTypeError):
|
with pytest.raises(yaml_loader.YamlTypeError):
|
||||||
yaml_loader.load_yaml_dict(YAML_CONFIG_FILE)
|
yaml_loader.load_yaml_dict(YAML_CONFIG_FILE)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user