Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_ListOf_error():
config = ConfigManager.from_dict({"bools": "t,f,badbool"})
with pytest.raises(InvalidValueError) as exc_info:
config("bools", parser=ListOf(bool))
assert (
str(exc_info.value) == 'ValueError: "badbool" is not a valid bool value\n'
"namespace=None key=bools requires a value parseable by "
def test_parse_bool_with_config():
config = ConfigManager.from_dict({"foo": "bar"})
# Test key is there, but value is bad
with pytest.raises(InvalidValueError) as excinfo:
config("foo", parser=bool)
assert (
str(excinfo.value) == 'ValueError: "bar" is not a valid bool value\n'
"namespace=None key=foo requires a value parseable by everett.manager.parse_bool"
)
# Test key is not there and default is bad
with pytest.raises(InvalidValueError) as excinfo:
config("phil", default="foo", parser=bool)
assert (
str(excinfo.value) == 'ValueError: "foo" is not a valid bool value\n'
"namespace=None key=phil requires a default value parseable by everett.manager.parse_bool"
#!/usr/bin/env python3
import logging
from everett import InvalidValueError
from everett.manager import ConfigManager
logging.basicConfig()
config = ConfigManager.from_dict({
'debug_mode': 'monkey'
})
try:
some_val = config('debug_mode', parser=bool)
except InvalidValueError:
# The "debug_mode" configuration value is incorrect--alert
# user in the logs.
logging.exception('gah!')
except Exception:
exc_info = sys.exc_info()
msg = build_msg(
"%(class)s: %(msg)s"
% {"class": exc_info[0].__name__, "msg": str(exc_info[1])},
"namespace=%(namespace)s key=%(key)s requires a value parseable by %(parser)s"
% { # noqa
"namespace": use_namespace,
"key": key,
"parser": qualname(parser),
},
doc,
self.doc,
)
raise InvalidValueError(msg, namespace, key, parser)
# Return the default if there is one
if default is not NO_VALUE:
try:
parsed_val = parser(default)
logger.debug(
"Returning default raw: %r, parsed: %r", default, parsed_val
)
return parsed_val
except ConfigurationError:
# Re-raise ConfigurationError and friends since that's
# what we want to be raising.
raise
except Exception:
# FIXME(willkg): This is a programmer error--not a user
# configuration error. We might want to denote that better.