rust errors
Error handling in Rust is a mess right now. Should I use error_chain or failure? It is complicated.
For real applications I definitely want errors to bubble all the way up to
main
, and I’ll lay down the boilerplate to wrap all possible errors and
output useful error messages.
However, for one-offs I would rather just quit with a simple error.
If you are in main
, it will kindly print a Debug
formatted error:
use std::fs::File;
fn main() -> std::io::Result<()> {
let mut file = File::open("foo.txt")?;
Ok(())
}
$ ./foo
Error: Os { code: 2, kind: NotFound, message: "No such file or directory" }
Not pretty, but adequate.
What if you aren’t in main
or in a function that bubbles Result
?
Maybe something like:
let mut file = File::open("foo.txt")
.unwrap_or_else(|e| {
eprintln!("{}", e);
std::process::exit(2);
});
It quickly gets tedious adding these silly blocks everywhere. I stumbled on an alternative.
Simply implement an extra method on Results
:
use std::fmt::Display;
pub trait OrDie<T> {
fn or_die(self, msg: &str) -> T;
}
impl<T, E: Display> OrDie<T> for Result<T, E> {
fn or_die(self, msg: &str) -> T {
match self {
Ok(t) => t,
Err(e) => {
eprintln!("{}: {}", msg, e);
std::process::exit(1);
}
}
}
}
Then you can just chain .or_die("some message")
to any Result
.
let mut file = File::open("foo.txt").or_die("foo.txt");
$ ./foo
foo.txt: No such file or directory (os error 2)