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

Raise a more specific exception? #16

Open
leonsmith opened this issue Jul 25, 2023 · 1 comment
Open

Raise a more specific exception? #16

leonsmith opened this issue Jul 25, 2023 · 1 comment

Comments

@leonsmith
Copy link

Thanks for the lib, much cleaner & typed compared to environs

My only gripe is there are a bunch of places in the code where a generic exception is raised.

Would you be open to subclassing it and rasing specific exceptions instead?

This is my gut reaction looking at the code

https://github.com/hukkin/typenv/blob/master/src/typenv/__init__.py#L62 -> TypeError (std lib)
https://github.com/hukkin/typenv/blob/master/src/typenv/__init__.py#L122 -> MissingEnvironmentVariable
https://github.com/hukkin/typenv/blob/master/src/typenv/__init__.py#L131 -> TypeError (std lib)
https://github.com/hukkin/typenv/blob/master/src/typenv/__init__.py#L413 -> ValidationError

@hukkin
Copy link
Owner

hukkin commented Aug 29, 2024

Hey, thanks for the issue!

I don't agree with TypeError for line 62 and 131. IMO TypeError should be be raised for programmatic errors when an incorrect type is used as function argument, e.g. in theory if we read a non str value from os.environ (which should never happen) . This is very different from typenv being unable to match the str to another type using its own set of parsing rules.

We could use custom types though for all the exceptions you mention. The reason we don't is because I couldn't think of a good reason to do so. Generally I think any exception from typenv is an error that should not be caught, as it means your env variables are bad. The variables should be fixed before trying again.

Custom exceptions only make sense if they are caught and handled. What's the use case for doing so?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants