From 9b7cf64e57b7181caf51a3ea867cf5021ab687f0 Mon Sep 17 00:00:00 2001 From: Prasanna Loganathar Date: Thu, 10 Oct 2019 01:29:56 +0530 Subject: [PATCH 1/2] add slightly improved main example A fairly common `print_error` implementation as a part of the main example. --- README.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/README.md b/README.md index 67437cb0..63c4d677 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,21 @@ fn process_data() -> Result<()> { fn unpack_config(data: &str) -> &str { "/some/path/that/does/not/exist" } + +fn main() { + if let Err(e) = process_data() { + print_error(&e); + } +} + +fn print_error(e: &dyn std::error::Error) { + eprintln!("error: {}", e); + let mut cause = e.source(); + while let Some(e) = cause { + eprintln!("caused by: {}", e); + cause = e.source(); + } +} ``` Please see [the documentation][Doc] and the [user's guide][Guide] for From 5a499242670cfd802505d26028cb911ca8891c7c Mon Sep 17 00:00:00 2001 From: Prasanna Loganathar Date: Thu, 10 Oct 2019 08:33:23 +0530 Subject: [PATCH 2/2] update readme to use ErrorCompat::iter_chain Blocked on: https://github.com/shepmaster/snafu/issues/187 --- README.md | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 63c4d677..c97e3911 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ SNAFU is a library to easily assign underlying errors into domain-specific errors while adding context. ```rust -use snafu::{ResultExt, Snafu}; +use snafu::{ResultExt, Snafu, ErrorCompat}; use std::{fs, io, path::PathBuf}; #[derive(Debug, Snafu)] @@ -38,16 +38,10 @@ fn unpack_config(data: &str) -> &str { fn main() { if let Err(e) = process_data() { - print_error(&e); - } -} - -fn print_error(e: &dyn std::error::Error) { - eprintln!("error: {}", e); - let mut cause = e.source(); - while let Some(e) = cause { - eprintln!("caused by: {}", e); - cause = e.source(); + eprintln!("error: {}", &e); + for cause in ErrorCompat::iter_chain(&e) { + eprintln!("caused by: {}", cause); + } } } ```