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

Attach cause to ImproperlyConfigured exception raised for missing environment variables #360

Closed
SebastiaanZ opened this issue Dec 30, 2021 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@SebastiaanZ
Copy link
Contributor

SebastiaanZ commented Dec 30, 2021

I'd like to propose to add the original exception as the cause of the ImproperlyConfigured exception that's raised when an environment variable is missing using raise ... from ...:

        try:
            value = self.ENVIRON[var]
        except KeyError as exc:
            if default is self.NOTSET:
                error_msg = "Set the {} environment variable".format(var)
                raise ImproperlyConfigured(error_msg) from exc

The original:

try:
value = self.ENVIRON[var]
except KeyError:
if default is self.NOTSET:
error_msg = "Set the {} environment variable".format(var)
raise ImproperlyConfigured(error_msg)

Note: Since it's a two-line change, I've opened #361 to introduce this change. I know that the proposal hasn't been discussed yet, but I figured not a lot of work is lost if it's rejected.

Background

The main difference is that this will mark the original KeyError as the explicit cause of the ImproperlyConfigered, instead of marking the ImproperlyConfigured as another exception that "happened to happen" during the handling of the KeyError. (A better explanation is given by Martijn Pieters here).

Differences for end-users

The most notable difference is the output. The proposed change would change the traceback output to:

Traceback (most recent call last):
  (...)
KeyError: 'missing_key'

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

Traceback (most recent call last):
  (...)
environ.compat.ImproperlyConfigured: Set the missing_key environment variable

instead of

Traceback (most recent call last):
  (...)
KeyError: 'missing_key'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  (...)
environ.compat.ImproperlyConfigured: Set the missing_key environment variable

Notice that the middle line that indicates the relationship between the two exceptions has changed.

Additionally, the original exception will now be attached to the ImproperlyConfigured exception under the __cause__ attribute instead of the __context__ attribute.

@sergeyklay sergeyklay self-assigned this Jan 2, 2022
@sergeyklay sergeyklay added the enhancement New feature or request label Jan 2, 2022
@sergeyklay
Copy link
Collaborator

Implemented in #361. Thank you for contributing!

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

2 participants