From 70fdc3c3f8a1250fb5476d26d24f0ad21985b491 Mon Sep 17 00:00:00 2001 From: Jessica Hamilton Date: Fri, 22 Dec 2023 14:58:30 +1300 Subject: [PATCH] web_server.py: return empty content when file doesn't exist (#5980) Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com> --- esphome/dashboard/web_server.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/esphome/dashboard/web_server.py b/esphome/dashboard/web_server.py index 4552aebf7b..c4b84d3fe3 100644 --- a/esphome/dashboard/web_server.py +++ b/esphome/dashboard/web_server.py @@ -792,13 +792,22 @@ class EditRequestHandler(BaseHandler): """Get the content of a file.""" loop = asyncio.get_running_loop() filename = settings.rel_path(configuration) - content = await loop.run_in_executor(None, self._read_file, filename) - self.write(content) + content = await loop.run_in_executor( + None, self._read_file, filename, configuration + ) + if content is not None: + self.write(content) - def _read_file(self, filename: str) -> bytes: + def _read_file(self, filename: str, configuration: str) -> bytes | None: """Read a file and return the content as bytes.""" - with open(file=filename, encoding="utf-8") as f: - return f.read() + try: + with open(file=filename, encoding="utf-8") as f: + return f.read() + except FileNotFoundError: + if configuration in const.SECRETS_FILES: + return "" + self.set_status(404) + return None def _write_file(self, filename: str, content: bytes) -> None: """Write a file with the given content."""