-
Notifications
You must be signed in to change notification settings - Fork 47
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
dotenv::var()'s error should report the variable that caused it #48
Comments
It's generally considered bad practice to include the arguments in your error type. Users want to be able to return errors about local values, and don't want to make redundant allocations. User facing errors, on the other hand, do need this context. Your UI can include logic to report missing variables itself: pub fn var<K: AsRef<OsStr>>(key: K) -> String {
let s = key.as_ref();
match env::var(&s) {
Ok(s) => s,
Err(e) => {
eprintln!("Couldn't get {}: {}", s.to_string_lossy(), e);
std::process::exit(1);
}
}
} |
We do try to wrap calls to Eliminating allocations where possible is a noble effort but we have to consider the context of where this function is usually called. It's never really called in hot loops or more than a handful of times in an entire application. Does it really matter if it allocates or not? If the user ignores the error value I'd be willing to bet the allocation is elided entirely in release mode. |
Alocations should be avoided if they are redundant, but in this case, I think they are justified.
I believe that errors should be able to have costful operations if it improves debugging for the developer or final user, because these errors usually occur only once and the program exits. |
Currently this forwards the underlying
std::env::VarError
which has very unhelpful error messages: https://doc.rust-lang.org/src/std/env.rs.html#274-283Namely, it doesn't report the variable that caused the error, which is useless for almost all intents and purposes unless you only ever invoke exactly it once in your application; even then, if someone tries to use your application that isn't familiar with this, they just get
environment variable not found
orenvironment variable was not valid unicode: <generic Utf8Error message>
.Unfortunately, this can't be changed in the stdlib for backcompat purposes since
VarError
is an enum, but I thinkdotenv::var()
should definitely return a more useful error here, which gives it additional utility as well.The text was updated successfully, but these errors were encountered: