From 6dc32ea82553029f58b8f128fce63be6674d02de Mon Sep 17 00:00:00 2001 From: davidemarcoli Date: Tue, 4 Jun 2024 18:02:24 +0200 Subject: [PATCH 1/3] Use default settings when the provided settings file can't be parsed --- backend/program/settings/manager.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/backend/program/settings/manager.py b/backend/program/settings/manager.py index 14886823..a57a6ae0 100644 --- a/backend/program/settings/manager.py +++ b/backend/program/settings/manager.py @@ -40,12 +40,14 @@ def load(self, settings_dict: dict | None = None): logger.warning( f"Error loading settings: {e}, initializing with default settings" ) - raise + self.settings = AppModel() + self.notify_observers() except json.JSONDecodeError as e: logger.warning( f"Error parsing settings file: {e}, initializing with default settings" ) - raise + self.settings = AppModel() + self.notify_observers() except FileNotFoundError: logger.warning(f"Error loading settings: {self.settings_file} does not exist") raise From dcd5067a8c6264b18bb18cd2bd8df12e6010f263 Mon Sep 17 00:00:00 2001 From: davidemarcoli Date: Tue, 4 Jun 2024 18:14:07 +0200 Subject: [PATCH 2/3] Improve error handling --- backend/program/settings/manager.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/backend/program/settings/manager.py b/backend/program/settings/manager.py index a57a6ae0..7caa3507 100644 --- a/backend/program/settings/manager.py +++ b/backend/program/settings/manager.py @@ -38,16 +38,14 @@ def load(self, settings_dict: dict | None = None): self.settings = AppModel.model_validate(settings_dict) except ValidationError as e: logger.warning( - f"Error loading settings: {e}, initializing with default settings" + f"Error validating settings: {e}" ) - self.settings = AppModel() - self.notify_observers() + exit(1) except json.JSONDecodeError as e: logger.warning( - f"Error parsing settings file: {e}, initializing with default settings" + f"Error parsing settings file: {e}" ) - self.settings = AppModel() - self.notify_observers() + exit(1) except FileNotFoundError: logger.warning(f"Error loading settings: {self.settings_file} does not exist") raise From 4cca08d73654d9da27d48cb0e63988ede32e1d92 Mon Sep 17 00:00:00 2001 From: davidemarcoli Date: Thu, 6 Jun 2024 13:24:57 +0200 Subject: [PATCH 3/3] Improve handling of invalid settings file --- backend/program/settings/manager.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/backend/program/settings/manager.py b/backend/program/settings/manager.py index 7caa3507..af5bccc4 100644 --- a/backend/program/settings/manager.py +++ b/backend/program/settings/manager.py @@ -37,15 +37,15 @@ def load(self, settings_dict: dict | None = None): settings_dict = json.loads(file.read()) self.settings = AppModel.model_validate(settings_dict) except ValidationError as e: - logger.warning( + logger.error( f"Error validating settings: {e}" ) - exit(1) + raise except json.JSONDecodeError as e: - logger.warning( + logger.error( f"Error parsing settings file: {e}" ) - exit(1) + raise except FileNotFoundError: logger.warning(f"Error loading settings: {self.settings_file} does not exist") raise