Remove error on broken pipe while outputting json

Serializing to json will only create an info and not an
error if interrupted by a broken pipe.
Addition to #15.
This commit is contained in:
Malte Tammena 2021-10-27 16:24:16 +02:00
parent 485676095e
commit 1a57a63a3b

View file

@ -307,6 +307,15 @@ fn get_sane_terminal_dimensions() -> (usize, usize) {
fn print_json<T: Serialize>(value: &T) -> Result<()> { fn print_json<T: Serialize>(value: &T) -> Result<()> {
let stdout = std::io::stdout(); let stdout = std::io::stdout();
let output = stdout.lock(); let output = stdout.lock();
serde_json::to_writer_pretty(output, value) let res = serde_json::to_writer_pretty(output, value);
.map_err(|why| Error::Serializing(why, "writing meals as json")) // This is done to catch broken pipe errors
match res {
Err(why) if why.is_io() => {
// Propagate as simple io error.
// BrokenPipe errors are catched in main
Err(Error::Io(why.into(), "serializing json"))
}
Err(other) => Err(Error::Serializing(other, "writing meals as json")),
Ok(()) => Ok(()),
}
} }