Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error message within prefixed() context #102

Closed
AGeekInside opened this issue Aug 23, 2019 · 3 comments · Fixed by #105
Closed

Error message within prefixed() context #102

AGeekInside opened this issue Aug 23, 2019 · 3 comments · Fixed by #105

Comments

@AGeekInside
Copy link

AGeekInside commented Aug 23, 2019

The error message does not prepend the prefix to error message output, when an error occurs in an env.prefixed() context. The code below illustrates the issue. The environment variable is named TEST_VAR, but the error message is showing VAR.

######@shim-dev:~/workspace/shim (environ-adoption) 
$ export TEST_VAR=-1
######@shim-dev:~/workspace/shim (environ-adoption) 
$ ipython
Python 3.6.5 (default, Aug 15 2019, 19:51:45) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.7.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: from environs import Env                                                                                                                                                                                                                                                     

In [2]: env = Env()                                                                                                                                                                                                                                                                  

In [3]: with env.prefixed("TEST_"): 
   ...:     invalid_test = env.int("VAR", validate=lambda n: n > 0) 
   ...:                                                                                                                                                                                                                                                                              
---------------------------------------------------------------------------
ValidationError                           Traceback (most recent call last)
~/.pyenv/versions/3.6.5/lib/python3.6/site-packages/environs.py in method(self, name, default, subcast, **kwargs)
     55         try:
---> 56             value = field.deserialize(value)
     57         except ma.ValidationError as error:

~/.pyenv/versions/3.6.5/lib/python3.6/site-packages/marshmallow/fields.py in deserialize(self, value, attr, data)
    265         output = self._deserialize(value, attr, data)
--> 266         self._validate(output)
    267         return output

~/.pyenv/versions/3.6.5/lib/python3.6/site-packages/marshmallow/fields.py in _validate(self, value)
    205         if errors:
--> 206             raise ValidationError(errors, **kwargs)
    207 

ValidationError: ['Invalid value.']

The above exception was the direct cause of the following exception:

EnvError                                  Traceback (most recent call last)
<ipython-input-3-be1d17efe506> in <module>
      1 with env.prefixed("TEST_"):
----> 2     invalid_test = env.int("VAR", validate=lambda n: n > 0)
      3 

~/.pyenv/versions/3.6.5/lib/python3.6/site-packages/environs.py in method(self, name, default, subcast, **kwargs)
     56             value = field.deserialize(value)
     57         except ma.ValidationError as error:
---> 58             raise EnvError('Environment variable "{}" invalid: {}'.format(name, error.args[0])) from error
     59         else:
     60             self._values[parsed_key] = value

EnvError: Environment variable "VAR" invalid: ['Invalid value.']

In [4]:  
@sloria
Copy link
Owner

sloria commented Aug 23, 2019

Thanks for reporting. I'd certainly review/merge a PR with the fix. From the traceback, it looks lke it might be as simple as changing

 raise EnvError('Environment variable "{}" invalid: {}'.format(name, error.args[0])) from error

to

 raise EnvError('Environment variable "{}" invalid: {}'.format(parsed_key, error.args[0])) from error

@AGeekInside
Copy link
Author

I'll take a look and see about creating a PR. Just noticed the issue as were refactoring a codebase to introduce environs, so we have an interest in a fix. :)

@sloria
Copy link
Owner

sloria commented Aug 23, 2019

Thanks!

sloria added a commit that referenced this issue Sep 22, 2019
sloria added a commit that referenced this issue Sep 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants