Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature request: Allow attaching Reports to Reports #208

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 7 additions & 118 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions color-eyre/examples/multiple_reports.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use color_eyre::Section;
use eyre::{eyre, Result};

fn main() -> Result<()> {
color_eyre::install()?;

let mut errors = vec![];

if let Err(err) = do_stuff() {
errors.push(err);
}

if let Err(err) = do_other_stuff() {
errors.push(err);
}

if !errors.is_empty() {
let mut err = eyre!("Program failure with {} errors!", errors.len());

for error in errors {
err = err.report(error);
}

return Err(err);
}

Ok(())
}

fn do_stuff() -> Result<()> {
let err = eyre!("Some thing gets wrong");

Err(err)
}

fn do_other_stuff() -> Result<()> {
let err = eyre!("Some other thing gets wrong");

Err(err)
}
46 changes: 46 additions & 0 deletions color-eyre/examples/multiple_reports_lazy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use color_eyre::Section;
use eyre::{eyre, Result};

fn main() -> Result<()> {
color_eyre::install()?;

let mut errors = vec![];

if let Err(err) = do_stuff() {
errors.push(err);
}

if let Err(err) = do_other_stuff() {
errors.push(err);
}

if !errors.is_empty() {
let final_report = eyre!("Program failure with {} errors!", errors.len());

let err = errors
.into_iter()
.fold(final_report, |report, r| report.with_report(|| r));

return Err(err);
}

Ok(())
}

fn do_stuff() -> Result<()> {
let parameters = (1, 2, 3);
let err = eyre!(
"Some thing gets wrong with parameters: \n-x: {} \n-y: {} \n-z: {}",
parameters.0,
parameters.1,
parameters.2,
);

Err(err)
}

fn do_other_stuff() -> Result<()> {
let err = eyre!("Some other thing gets wrong");

Err(err)
}
3 changes: 3 additions & 0 deletions color-eyre/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub struct Theme {
pub(crate) help_info_warning: Style,
pub(crate) help_info_suggestion: Style,
pub(crate) help_info_error: Style,
pub(crate) help_info_report: Style,
pub(crate) dependency_code: Style,
pub(crate) crate_code: Style,
pub(crate) code_hash: Style,
Expand Down Expand Up @@ -63,6 +64,7 @@ impl Theme {
help_info_warning: style().bright_yellow(),
help_info_suggestion: style().bright_cyan(),
help_info_error: style().bright_red(),
help_info_report: style().bright_red(),
dependency_code: style().green(),
crate_code: style().bright_red(),
code_hash: style().bright_black(),
Expand Down Expand Up @@ -91,6 +93,7 @@ impl Theme {
help_info_warning: style().bright_red(),
help_info_suggestion: style().blue(),
help_info_error: style().red(),
help_info_report: style().red(),
dependency_code: style().green(),
crate_code: style().red(),
code_hash: style().bright_black(),
Expand Down
53 changes: 53 additions & 0 deletions color-eyre/src/section/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,30 @@ impl Section for Report {
self
}

fn report(mut self, report: Report) -> Self::Return {
if let Some(handler) = self.handler_mut().downcast_mut::<crate::Handler>() {
handler
.sections
.push(HelpInfo::Report(Box::new(report), handler.theme));
}

self
}

fn with_report<F>(mut self, report: F) -> Self::Return
where
F: FnOnce() -> Report,
{
if let Some(handler) = self.handler_mut().downcast_mut::<crate::Handler>() {
let report = report();
handler
.sections
.push(HelpInfo::Report(Box::new(report), handler.theme));
}

self
}

fn suppress_backtrace(mut self, suppress: bool) -> Self::Return {
if let Some(handler) = self.handler_mut().downcast_mut::<crate::Handler>() {
handler.suppress_backtrace = suppress;
Expand Down Expand Up @@ -243,6 +267,19 @@ where
.map_err(|report| report.error(error()))
}

fn report(self, report: Report) -> Self::Return {
self.map_err(|error| error.into())
.map_err(|error_report| error_report.report(report))
}

fn with_report<F>(self, report: F) -> Self::Return
where
F: FnOnce() -> Report,
{
self.map_err(|error| error.into())
.map_err(|error_report| error_report.report(report()))
}

fn suppress_backtrace(self, suppress: bool) -> Self::Return {
self.map_err(|error| error.into())
.map_err(|report| report.suppress_backtrace(suppress))
Expand All @@ -255,6 +292,7 @@ pub(crate) enum HelpInfo {
Note(Box<dyn Display + Send + Sync + 'static>, Theme),
Warning(Box<dyn Display + Send + Sync + 'static>, Theme),
Suggestion(Box<dyn Display + Send + Sync + 'static>, Theme),
Report(Box<Report>, Theme),
}

impl Display for HelpInfo {
Expand Down Expand Up @@ -289,6 +327,20 @@ impl Display for HelpInfo {
write!(indented(f).ind(n), "{}", error.style(theme.help_info_error))?;
}

Ok(())
}
HelpInfo::Report(report, theme) => {
let chain_errors = report.chain();
write!(f, "Report:")?;
for (n, error) in chain_errors.enumerate() {
writeln!(f)?;
write!(
indented(f).ind(n),
"{}",
error.style(theme.help_info_report)
)?;
}

Ok(())
}
}
Expand All @@ -315,6 +367,7 @@ impl fmt::Debug for HelpInfo {
.field(&format_args!("{}", custom))
.finish(),
HelpInfo::Error(error, ..) => f.debug_tuple("Error").field(error).finish(),
HelpInfo::Report(report, ..) => f.debug_tuple("Report").field(report).finish(),
}
}
}
Loading