mirror of
https://github.com/sharkdp/hexyl.git
synced 2026-10-29 08:05:57 +00:00
feat: use anyhow for more structured error messages and better error reporting flexibility
This significantly changes the structure of potential output of
top-level errors. Before, errors that have a `source` were printed on a
single line, with the `source` omitted. Now, an error with a `source`
will be printed like:
```
Error: <`Display` impl of error>
Caused by:
<`Display` impl of `source` error>
```
If there are multiple `source` errors in a chain, then it will be of the
form:
```
Error: <`Display` impl of error>
Caused by:
0: <`Display` impl of first `source` error>
1: <`Display` impl of `source` of `source` error>
# etc.
```
Now, in practice this never happened before, since the only error
type that ever could return from `<binary>::run` was `std::io::Error`,
which has no `source`. However, `source`s can start being added if, say,
future work involved using `anyhow::Context::context` or custom errors
that implement `std::Error::source` are added to the codebase. I believe
this is a good choice going forward, but I definitely want to foster
discussion and acceptance for it in the open first!
This commit is contained in:
1 parent
a7892881f3
commit
2bdf5d768f
4 files changed
+14
-4
No files matched your search
+5
-3
@@ -9,9 +9,11 @@ use clap::{App, AppSettings, Arg};
|
||||
|
||||
use atty::Stream;
|
||||
|
||||
use anyhow::{anyhow, Error as AnyhowError};
|
||||
|
||||
use hexyl::{BorderStyle, Input, Printer};
|
||||
|
||||
fn run() -> Result<(), Box<dyn std::error::Error>> {
|
||||
fn run() -> Result<(), AnyhowError> {
|
||||
let app = App::new(crate_name!())
|
||||
.setting(AppSettings::ColorAuto)
|
||||
.setting(AppSettings::ColoredHelp)
|
||||
@@ -165,7 +167,7 @@ fn run() -> Result<(), Box<dyn std::error::Error>> {
|
||||
|
||||
let mut printer = Printer::new(&mut stdout_lock, show_color, border_style, squeeze);
|
||||
printer.display_offset(display_offset);
|
||||
printer.print_all(&mut reader)?;
|
||||
printer.print_all(&mut reader).map_err(|e| anyhow!(e))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -189,7 +191,7 @@ fn main() {
|
||||
_ => (),
|
||||
}
|
||||
} else {
|
||||
eprintln!("Error: {}", err);
|
||||
eprintln!("Error: {:?}", err);
|
||||
}
|
||||
std::process::exit(1);
|
||||
}
|
||||
|
||||
+1
-1
@@ -408,7 +408,7 @@ impl<'a, Writer: Write> Printer<'a, Writer> {
|
||||
pub fn print_all<Reader: Read>(
|
||||
&mut self,
|
||||
mut reader: Reader,
|
||||
) -> Result<(), Box<dyn std::error::Error>> {
|
||||
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||
let mut buffer = [0; BUFFER_SIZE];
|
||||
'mainloop: loop {
|
||||
let size = reader.read(&mut buffer)?;
|
||||
|
||||
Reference in new issue
Block a user