Skip to content

Commit

Permalink
Fix error message for prefixed var
Browse files Browse the repository at this point in the history
close #102
  • Loading branch information
sloria committed Sep 22, 2019
1 parent 8dd86d4 commit 4283419
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ def https_url(value):
return "https://" + value
```

Bug fixes:

- Fix error message for prefixed variables ([#102](https://github.com/sloria/environs/issues/102)).
Thanks [AGeekInside](https://github.com/AGeekInside) for reporting.

Other changes:

- _Backwards-incompatible_: Rename `Env.__parser_map__` to `Env.__custom_parsers__`.

## 5.2.1 (2019-08-08)
Expand Down
7 changes: 5 additions & 2 deletions environs.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ def method(
field = typing.cast(FieldFactory, field_or_factory)(subcast=subcast, missing=missing, **kwargs)
parsed_key, raw_value, proxied_key = self._get_from_environ(name, ma.missing)
self._fields[parsed_key] = field
source_key = proxied_key or parsed_key
if raw_value is ma.missing and field.missing is ma.missing:
raise EnvError('Environment variable "{}" not set'.format(proxied_key or parsed_key))
raise EnvError('Environment variable "{}" not set'.format(source_key))
if raw_value or raw_value == "":
value = raw_value
else:
Expand All @@ -63,7 +64,9 @@ def method(
try:
value = field.deserialize(value)
except ma.ValidationError as error:
raise EnvError('Environment variable "{}" invalid: {}'.format(name, error.args[0])) from error
raise EnvError(
'Environment variable "{}" invalid: {}'.format(source_key, error.args[0])
) from error
else:
self._values[parsed_key] = value
return value
Expand Down
5 changes: 5 additions & 0 deletions tests/test_environs.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,11 @@ def test_dump_with_prefixed(self, env):
env("NOT_FOUND", "mydefault") == "mydefault"
assert env.dump() == {"APP_STR": "foo", "APP_INT": 42, "APP_NOT_FOUND": "mydefault"}

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)


class TestNestedPrefix:
@pytest.fixture(autouse=True)
Expand Down

0 comments on commit 4283419

Please sign in to comment.