Description
I have been refactoring command-line program code to use the patterns implied by Improved Error Handling in Rust. Errors are bubbled up to main()
using the std::error::Error
and std::error::FromError
traits. This allows most user messaging to occur in a central location (especially panic-level failures), simplifying testing and internationalization.
One issue I have encountered has to do with missing environment variables. std::env::VarError
does not capture the the environment variable name (which std::env calls a key); therefore, the messages from Error::description()
and Display::fmt()
do not tell the user which environment variable is missing. To work around this issue, I have wrapped VarError
in another struct. It would be much simpler if VarError
captured the environment variable name and included it in the messages.
I realize this is likely to require heap allocation to save a copy of the name. In most (sane) cases, environment variables are accessed once during program initialization and the value stored for future use. Therefore, I believe the benefit of knowing which environment variable is missing outweighs the heap allocation cost.