How to use the environs.EnvError function in environs

To help you get started, we’ve selected a few environs examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github sloria / environs / tests / test_environs.py View on Github external
def enum_parser(value, choices):
            if value not in choices:
                raise environs.EnvError("Invalid!")
            return value
github sloria / environs / tests / test_environs.py View on Github external
def always_fail(value):
    raise environs.EnvError("something went wrong")
github sloria / environs / tests / test_environs.py View on Github external
def test_error_message_for_prefixed_var(self, env):
        with env.prefixed("APP_"):
            with pytest.raises(environs.EnvError, match='Environment variable "APP_INT" invalid'):
                env.int("INT", validate=lambda val: val < 42)
github sloria / environs / environs.py View on Github external
missing = kwargs.pop('missing', None) or default
        if isinstance(field_or_factory, type) and issubclass(field_or_factory, ma.fields.Field):
            field = field_or_factory(missing=missing, **kwargs)
        else:
            field = field_or_factory(subcast=subcast, missing=missing, **kwargs)
        self._fields[name] = field
        raw_value = os.environ.get(name, ma.missing)
        if raw_value is ma.missing and field.missing is ma.missing:
            raise EnvError('Environment variable "{}" not set'.format(name))
        value = raw_value or field.missing
        if preprocess:
            value = preprocess(value, subcast=subcast, **kwargs)
        try:
            value = field.deserialize(value)
        except ma.ValidationError as err:
            raise EnvError('Environment variable "{}" invalid: {}'.format(name, err.args[0]))
        else:
            self._values[name] = value
            return value
    method.__name__ = str(method_name)
github sloria / environs / environs.py View on Github external
def method(self, name, default=ma.missing, subcast=None, **kwargs):
        name = self._prefix + name if self._prefix else name
        raw_value = os.environ.get(name, default)
        if raw_value is ma.missing:
            raise EnvError('Environment variable "{}" not set'.format(name))
        value = func(raw_value, **kwargs)
        self._fields[name] = ma.fields.Field(**kwargs)
        self._values[name] = value
        return value
    method.__name__ = str(method_name)
github sloria / environs / examples / validation_example.py View on Github external
from environs import Env, EnvError
from marshmallow.validate import OneOf, Email, Length


os.environ["TTL"] = "-2"
os.environ["NODE_ENV"] = "invalid"
os.environ["EMAIL"] = "^_^"


env = Env()

# simple validator
try:
    env.int("TTL", validate=lambda n: n > 0)
except EnvError as err:
    print(err)

# marshmallow validator
try:
    env.str(
        "NODE_ENV", validate=OneOf(["production", "development"], error="NODE_ENV must be one of: {choices}")
    )
except EnvError as err:
    print(err)


# multiple validators
try:
    env.str("EMAIL", validate=[Length(min=4), Email()])
except EnvError as err:
    print(err)