Skip to content

Commit

Permalink
refactor - use ValitationError exception subclass to handle & format …
Browse files Browse the repository at this point in the history
…list of errors in message

Signed-off-by: David Martin <[email protected]>
  • Loading branch information
David Martin committed Dec 18, 2019
1 parent d20c3ca commit b056040
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
29 changes: 28 additions & 1 deletion chaoslib/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
__all__ = ["ChaosException", "InvalidExperiment", "InvalidActivity",
"ActivityFailed", "DiscoveryFailed", "InvalidSource",
"InterruptExecution", "ControlPythonFunctionLoadingError",
"InvalidControl"]
"InvalidControl", "ValidationError"]


class ChaosException(Exception):
Expand Down Expand Up @@ -44,3 +44,30 @@ class InterruptExecution(ChaosException):

class InvalidControl(ChaosException):
pass


class ValidationError(ChaosException):
def __init__(self, msg, errors, *args, **kwargs):
"""
:param msg: exception message
:param errors: single error as string or list of errors/exceptions
"""
if isinstance(errors, str):
errors = [errors]
self.errors = errors
super().__init__(msg, *args, **kwargs)

def __str__(self) -> str:
errors = self.errors
nb_errors = len(errors)
err_msg = super().__str__()
return (
"{msg}{dot} {nb} validation error{plural}:\n"
" - {errors}".format(
msg=err_msg,
dot="" if err_msg.endswith(".") else ".",
nb=nb_errors,
plural="" if nb_errors == 1 else "s",
errors="\n - ".join([str(err) for err in errors])
)
)
14 changes: 7 additions & 7 deletions chaoslib/experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
cleanup_global_controls
from chaoslib.deprecation import warn_about_deprecated_features
from chaoslib.exceptions import ActivityFailed, ChaosException, \
InterruptExecution, InvalidActivity, InvalidExperiment
InterruptExecution, InvalidActivity, InvalidExperiment, ValidationError
from chaoslib.extension import validate_extensions
from chaoslib.configuration import load_configuration
from chaoslib.hypothesis import ensure_hypothesis_is_valid, \
Expand Down Expand Up @@ -55,10 +55,14 @@ def ensure_experiment_is_valid(experiment: Experiment):
"""
logger.info("Validating the experiment's syntax")

full_validation_msg = 'Experiment is not valid, ' \
'please fix the following errors'
errors = []

if not experiment:
raise InvalidExperiment("an empty experiment is not an experiment")
# empty experiment, cannot continue validation any further
raise ValidationError(full_validation_msg,
"an empty experiment is not an experiment")

if not experiment.get("title"):
errors.append(InvalidExperiment("experiment requires a title"))
Expand Down Expand Up @@ -103,11 +107,7 @@ def ensure_experiment_is_valid(experiment: Experiment):
errors.extend(validate_controls(experiment))

if errors:
full_validation_msg = 'Experiment is not valid, ' \
'please fix the following errors:'
for error in errors:
full_validation_msg += '\n- {}'.format(error)
raise InvalidExperiment(full_validation_msg)
raise ValidationError(full_validation_msg, errors)

logger.info("Experiment looks valid")

Expand Down

0 comments on commit b056040

Please sign in to comment.