Settings loader, pass error messages to exception raise

So we can get the actual error message in the exception if logging is all off
This commit is contained in:
Clemens Schwaighofer
2025-12-24 10:08:38 +09:00
parent 17e8c76b94
commit d224876a8e
3 changed files with 29 additions and 1 deletions

View File

@@ -53,6 +53,9 @@ class SettingsLoader:
# for check settings, abort flag
self.__check_settings_abort: bool = False
# error messages for raise ValueError
self.__error_msg: list[str] = []
# MARK: load settings
def load_settings(
self,
@@ -273,7 +276,12 @@ class SettingsLoader:
error = True
self.__print(f"[!] Missing content entry for: {entry}", 'ERROR')
if error is True:
raise ValueError(self.__print("[!] Missing or incorrect settings data. Cannot proceed", 'CRITICAL'))
raise ValueError(
self.__print(
"[!] Missing or incorrect settings data. Cannot proceed: " + "; ".join(self.__error_msg),
'CRITICAL'
)
)
# set empty
for [entry, empty_set] in entry_set_empty.items():
# if set, skip, else set to empty value
@@ -567,6 +575,9 @@ class SettingsLoader:
if self.log is None or self.always_print:
if print_error:
print(msg)
if level == 'ERROR':
# remove any prefix [!] for error message list
self.__error_msg.append(msg.replace('[!] ', '').strip())
return msg

View File

@@ -37,3 +37,6 @@ email_bad=gii@bar.com
[LoadTest]
a.b.c=foo
d:e:f=bar
[ErrorTest]
some_value=42

View File

@@ -125,6 +125,20 @@ def main():
except ValueError as e:
print(f"Could not load settings: {e}")
try:
config_load = 'ErrorTest'
config_data = sl.load_settings(
config_load,
{
"some_value": [
"check:string.email.basic",
],
}
)
print(f"[{config_load}] Load: {config_load} -> {dump_data(config_data)}")
except ValueError as e:
print(f"Could not load settings: {e}")
if __name__ == "__main__":
main()