From 7ba6954af86f028457f0c2591faa56df7886c8ce Mon Sep 17 00:00:00 2001 From: checktheroads Date: Sun, 5 Jan 2020 00:34:44 -0700 Subject: [PATCH] improve exception & log handling --- hyperglass/configuration/__init__.py | 1 + hyperglass/exceptions.py | 23 +++++++++++++++++++---- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/hyperglass/configuration/__init__.py b/hyperglass/configuration/__init__.py index 33bf364..dbe4e79 100644 --- a/hyperglass/configuration/__init__.py +++ b/hyperglass/configuration/__init__.py @@ -145,6 +145,7 @@ try: except ValidationError as validation_errors: errors = validation_errors.errors() + log.error(errors) for error in errors: raise ConfigInvalid( field=": ".join([str(item) for item in error["loc"]]), diff --git a/hyperglass/exceptions.py b/hyperglass/exceptions.py index aa9fd6b..c7423eb 100644 --- a/hyperglass/exceptions.py +++ b/hyperglass/exceptions.py @@ -95,7 +95,9 @@ class HyperglassError(Exception): class _UnformattedHyperglassError(HyperglassError): """Base exception class for freeform error messages.""" - def __init__(self, unformatted_msg="", alert="warning", **kwargs): + _alert = "warning" + + def __init__(self, unformatted_msg="", alert=None, **kwargs): """Format error message with keyword arguments. Keyword Arguments: @@ -104,24 +106,37 @@ class _UnformattedHyperglassError(HyperglassError): keywords {list} -- 'Important' keywords (default: {None}) """ self._message = unformatted_msg.format(**kwargs) - self._alert = alert + self._alert = alert or self._alert self._keywords = list(kwargs.values()) super().__init__( message=self._message, alert=self._alert, keywords=self._keywords ) +class _PredefinedHyperglassError(HyperglassError): + _message = "undefined" + _alert = "warning" + + def __init__(self, alert=None, **kwargs): + self._fmt_msg = self._message.format(**kwargs) + self._alert = alert or self._alert + self._keywords = list(kwargs.values()) + super().__init__( + message=self._fmt_msg, alert=self._alert, keywords=self._keywords + ) + + class ConfigError(_UnformattedHyperglassError): """Raised for generic user-config issues.""" -class ConfigInvalid(_UnformattedHyperglassError): +class ConfigInvalid(_PredefinedHyperglassError): """Raised when a config item fails type or option validation.""" _message = 'The value field "{field}" is invalid: {error_msg}' -class ConfigMissing(_UnformattedHyperglassError): +class ConfigMissing(_PredefinedHyperglassError): """Raised when a required config file or item is missing or undefined.""" _message = (