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

Wrap the KeyError exception for missing variables #5

Open
amrishparmar opened this issue Aug 15, 2023 · 0 comments
Open

Wrap the KeyError exception for missing variables #5

amrishparmar opened this issue Aug 15, 2023 · 0 comments
Labels
enhancement New feature or request

Comments

@amrishparmar
Copy link
Collaborator

The https://docs.python.org/3/library/string.html#string.Template.substitute method will throw a KeyError if values are missing, which is fine, but results in confusing behaviour for a user who sees an error message from the underlying implementation detail. This exception should be caught and have a friendlier message emitted from this library

Example of trace:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/amrish/miniconda3/envs/pedro2/lib/python3.11/site-packages/env_templating/templating.py", line 63, in update_environment_variables
    _write_env_file_with_substitutions(temp_output_file_path, template, substitutions)
  File "/home/amrish/miniconda3/envs/pedro2/lib/python3.11/site-packages/env_templating/templating.py", line 23, in _write_env_file_with_substitutions
    new_env_fp.write(env_template.substitute(substitutions))
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/amrish/miniconda3/envs/pedro2/lib/python3.11/string.py", line 121, in substitute
    return self.pattern.sub(convert, self.template)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/amrish/miniconda3/envs/pedro2/lib/python3.11/string.py", line 114, in convert
    return str(mapping[named])
               ~~~~~~~^^^^^^^
KeyError: 'DB_HOST'
@amrishparmar amrishparmar added the enhancement New feature or request label Aug 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant