Improve config validation logging

This corrects the formatting on the error messages, and adds an error
condition when a type that should be a str or bool is specified as a
list or dict.
This commit is contained in:
Erik Johnson 2016-02-18 17:45:21 -06:00
parent cd3400e67e
commit 795008bad1

View File

@ -730,22 +730,40 @@ def _validate_opts(opts):
if isinstance(val, VALID_OPTS[key]): if isinstance(val, VALID_OPTS[key]):
continue continue
else: else:
errors.append(err.format(key, val, type(val), 'list')) errors.append(
err.format(key, val, type(val).__name__, 'list')
)
if isinstance(VALID_OPTS[key](), dict): if isinstance(VALID_OPTS[key](), dict):
if isinstance(val, VALID_OPTS[key]): if isinstance(val, VALID_OPTS[key]):
continue continue
else: else:
errors.append(err.format(key, val, type(val), 'dict')) errors.append(
err.format(key, val, type(val).__name__, 'dict')
)
else: else:
try: try:
VALID_OPTS[key](val) VALID_OPTS[key](val)
if isinstance(val, (list, dict)):
# We'll only get here if VALID_OPTS[key] is str or
# bool, and the passed value is a list/dict. Attempting
# to run int() or float() on a list/dict will raise an
# exception, but running str() or bool() on it will
# pass despite not being the correct type.
errors.append(
err.format(
key,
val,
type(val).__name__,
VALID_OPTS[key].__name__
)
)
except ValueError: except ValueError:
errors.append( errors.append(
err.format(key, val, type(val), VALID_OPTS[key]) err.format(key, val, type(val).__name__, VALID_OPTS[key])
) )
except TypeError: except TypeError:
errors.append( errors.append(
err.format(key, val, type(val), VALID_OPTS[key]) err.format(key, val, type(val).__name__, VALID_OPTS[key])
) )
for error in errors: for error in errors: