mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-31 07:03:55 +00:00 
			
		
		
		
	Ensure filename is shown when YAML raises an error (#6139)
* Ensure filename is shown when YAML raises an error fixes #5423 fixes #5377 * Ensure filename is shown when YAML raises an error fixes #5423 fixes #5377 * Ensure filename is shown when YAML raises an error fixes #5423 fixes #5377 * Ensure filename is shown when YAML raises an error fixes #5423 fixes #5377 * Ensure filename is shown when YAML raises an error fixes #5423 fixes #5377
This commit is contained in:
		| @@ -7,6 +7,7 @@ import logging | |||||||
| import math | import math | ||||||
| import os | import os | ||||||
| import uuid | import uuid | ||||||
|  | from io import TextIOWrapper | ||||||
| from typing import Any | from typing import Any | ||||||
|  |  | ||||||
| import yaml | import yaml | ||||||
| @@ -19,7 +20,7 @@ except ImportError: | |||||||
|     FastestAvailableSafeLoader = PurePythonLoader |     FastestAvailableSafeLoader = PurePythonLoader | ||||||
|  |  | ||||||
| from esphome import core | from esphome import core | ||||||
| from esphome.config_helpers import Extend, Remove, read_config_file | from esphome.config_helpers import Extend, Remove | ||||||
| from esphome.core import ( | from esphome.core import ( | ||||||
|     CORE, |     CORE, | ||||||
|     DocumentRange, |     DocumentRange, | ||||||
| @@ -418,19 +419,26 @@ def load_yaml(fname: str, clear_secrets: bool = True) -> Any: | |||||||
|  |  | ||||||
| def _load_yaml_internal(fname: str) -> Any: | def _load_yaml_internal(fname: str) -> Any: | ||||||
|     """Load a YAML file.""" |     """Load a YAML file.""" | ||||||
|     content = read_config_file(fname) |  | ||||||
|     try: |     try: | ||||||
|         return _load_yaml_internal_with_type(ESPHomeLoader, fname, content) |         with open(fname, encoding="utf-8") as f_handle: | ||||||
|  |             try: | ||||||
|  |                 return _load_yaml_internal_with_type(ESPHomeLoader, fname, f_handle) | ||||||
|             except EsphomeError: |             except EsphomeError: | ||||||
|                 # Loading failed, so we now load with the Python loader which has more |                 # Loading failed, so we now load with the Python loader which has more | ||||||
|                 # readable exceptions |                 # readable exceptions | ||||||
|         return _load_yaml_internal_with_type(ESPHomePurePythonLoader, fname, content) |                 # Rewind the stream so we can try again | ||||||
|  |                 f_handle.seek(0, 0) | ||||||
|  |                 return _load_yaml_internal_with_type( | ||||||
|  |                     ESPHomePurePythonLoader, fname, f_handle | ||||||
|  |                 ) | ||||||
|  |     except (UnicodeDecodeError, OSError) as err: | ||||||
|  |         raise EsphomeError(f"Error reading file {fname}: {err}") from err | ||||||
|  |  | ||||||
|  |  | ||||||
| def _load_yaml_internal_with_type( | def _load_yaml_internal_with_type( | ||||||
|     loader_type: type[ESPHomeLoader] | type[ESPHomePurePythonLoader], |     loader_type: type[ESPHomeLoader] | type[ESPHomePurePythonLoader], | ||||||
|     fname: str, |     fname: str, | ||||||
|     content: str, |     content: TextIOWrapper, | ||||||
| ) -> Any: | ) -> Any: | ||||||
|     """Load a YAML file.""" |     """Load a YAML file.""" | ||||||
|     loader = loader_type(content) |     loader = loader_type(content) | ||||||
|   | |||||||
							
								
								
									
										12
									
								
								tests/unit_tests/fixtures/yaml_util/missing_comp.yaml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								tests/unit_tests/fixtures/yaml_util/missing_comp.yaml
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,12 @@ | |||||||
|  | esphome: | ||||||
|  |   name: test | ||||||
|  |  | ||||||
|  | esp32: | ||||||
|  |   board: esp32dev | ||||||
|  |  | ||||||
|  | wifi: | ||||||
|  |   ap: ~ | ||||||
|  |  | ||||||
|  | image: | ||||||
|  |   - id: its_a_bug | ||||||
|  |     file: "mdi:bug" | ||||||
| @@ -22,3 +22,23 @@ def test_loading_a_broken_yaml_file(fixture_path): | |||||||
|         yaml_util.load_yaml(yaml_file) |         yaml_util.load_yaml(yaml_file) | ||||||
|     except EsphomeError as err: |     except EsphomeError as err: | ||||||
|         assert "broken_included.yaml" in str(err) |         assert "broken_included.yaml" in str(err) | ||||||
|  |  | ||||||
|  |  | ||||||
|  | def test_loading_a_yaml_file_with_a_missing_component(fixture_path): | ||||||
|  |     """Ensure we show the filename for a yaml file with a missing component.""" | ||||||
|  |     yaml_file = fixture_path / "yaml_util" / "missing_comp.yaml" | ||||||
|  |  | ||||||
|  |     try: | ||||||
|  |         yaml_util.load_yaml(yaml_file) | ||||||
|  |     except EsphomeError as err: | ||||||
|  |         assert "missing_comp.yaml" in str(err) | ||||||
|  |  | ||||||
|  |  | ||||||
|  | def test_loading_a_missing_file(fixture_path): | ||||||
|  |     """We throw EsphomeError when loading a missing file.""" | ||||||
|  |     yaml_file = fixture_path / "yaml_util" / "missing.yaml" | ||||||
|  |  | ||||||
|  |     try: | ||||||
|  |         yaml_util.load_yaml(yaml_file) | ||||||
|  |     except EsphomeError as err: | ||||||
|  |         assert "missing.yaml" in str(err) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user