confighelper: clean up warnings

Don't generate additional "unparsed option" warnings when
a component fails to load.  When an error is encountered,
include the original error message in the subsequent
ConfigError.

Signed-off-by:  Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
Eric Callahan 2023-12-23 15:22:23 -05:00
parent ad1666bb2c
commit dbbb07c68e
No known key found for this signature in database
GPG Key ID: 5A1EB336DFB4C71B
1 changed files with 13 additions and 3 deletions

View File

@ -47,6 +47,7 @@ if TYPE_CHECKING:
ConfigVal = Union[None, int, float, bool, str, dict, list] ConfigVal = Union[None, int, float, bool, str, dict, list]
DOCS_URL = "https://moonraker.readthedocs.io/en/latest" DOCS_URL = "https://moonraker.readthedocs.io/en/latest"
CFG_ERROR_KEY = "__CONFIG_ERROR__"
class ConfigError(Exception): class ConfigError(Exception):
pass pass
@ -143,12 +144,15 @@ class ConfigHelper:
val = func(section, option) val = func(section, option)
except (configparser.NoOptionError, configparser.NoSectionError) as e: except (configparser.NoOptionError, configparser.NoSectionError) as e:
if default is Sentinel.MISSING: if default is Sentinel.MISSING:
self.parsed[self.section][CFG_ERROR_KEY] = True
raise ConfigError(str(e)) from None raise ConfigError(str(e)) from None
val = default val = default
section = self.section section = self.section
except Exception as e: except Exception as e:
self.parsed[self.section][CFG_ERROR_KEY] = True
raise ConfigError( raise ConfigError(
f"Error parsing option ({option}) from section [{self.section}]" f"[{self.section}]: Option '{option}' encountered the following "
f"error while parsing: {e}"
) from e ) from e
else: else:
if deprecate: if deprecate:
@ -482,9 +486,14 @@ class ConfigHelper:
f"Unparsed config section [{sect}] detected. This " f"Unparsed config section [{sect}] detected. This "
"may be the result of a component that failed to " "may be the result of a component that failed to "
"load. In the future this will result in a startup " "load. In the future this will result in a startup "
"error.") "error."
)
continue continue
parsed_opts = self.parsed[sect] parsed_opts = self.parsed[sect]
if CFG_ERROR_KEY in parsed_opts:
# Skip validation for sections that have encountered an error,
# as this will always result in unparsed options.
continue
for opt, val in self.config.items(sect): for opt, val in self.config.items(sect):
if opt not in parsed_opts: if opt not in parsed_opts:
self.server.add_warning( self.server.add_warning(
@ -492,7 +501,8 @@ class ConfigHelper:
f"section [{sect}]. This may be an option no longer " f"section [{sect}]. This may be an option no longer "
"available or could be the result of a module that " "available or could be the result of a module that "
"failed to load. In the future this will result " "failed to load. In the future this will result "
"in a startup error.") "in a startup error."
)
def create_backup(self) -> None: def create_backup(self) -> None:
cfg_path = self.server.get_app_args()["config_file"] cfg_path = self.server.get_app_args()["config_file"]