fix: Do not panic on multiple install calls

This commit is contained in:
Jayson Reis 2022-07-26 09:27:37 +02:00
parent ede865079d
commit 48037df311
2 changed files with 12 additions and 4 deletions

View File

@ -4,6 +4,7 @@ use crate::{
section::PanicMessage, section::PanicMessage,
writers::{EnvSection, WriterExt}, writers::{EnvSection, WriterExt},
}; };
use eyre::WrapErr;
use fmt::Display; use fmt::Display;
use indenter::{indented, Format}; use indenter::{indented, Format};
use owo_colors::{style, OwoColorize, Style}; use owo_colors::{style, OwoColorize, Style};
@ -700,7 +701,7 @@ impl HookBuilder {
/// Install the given Hook as the global error report hook /// Install the given Hook as the global error report hook
pub fn install(self) -> Result<(), crate::eyre::Report> { pub fn install(self) -> Result<(), crate::eyre::Report> {
let (panic_hook, eyre_hook) = self.into_hooks(); let (panic_hook, eyre_hook) = self.into_hooks()?;
eyre_hook.install()?; eyre_hook.install()?;
panic_hook.install(); panic_hook.install();
Ok(()) Ok(())
@ -714,7 +715,7 @@ impl HookBuilder {
/// Create a `PanicHook` and `EyreHook` from this `HookBuilder`. /// Create a `PanicHook` and `EyreHook` from this `HookBuilder`.
/// This can be used if you want to combine these handlers with other handlers. /// This can be used if you want to combine these handlers with other handlers.
pub fn into_hooks(self) -> (PanicHook, EyreHook) { pub fn into_hooks(self) -> Result<(PanicHook, EyreHook), crate::eyre::Report> {
let theme = self.theme; let theme = self.theme;
#[cfg(feature = "issue-url")] #[cfg(feature = "issue-url")]
let metadata = Arc::new(self.issue_metadata); let metadata = Arc::new(self.issue_metadata);
@ -753,9 +754,9 @@ impl HookBuilder {
}; };
#[cfg(feature = "capture-spantrace")] #[cfg(feature = "capture-spantrace")]
color_spantrace::set_theme(self.theme.into()).expect("could not set the provided `Theme` via `color_spantrace::set_theme` globally as another was already set"); color_spantrace::set_theme(self.theme.into()).wrap_err("could not set the provided `Theme` via `color_spantrace::set_theme` globally as another was already set")?;
(panic_hook, eyre_hook) Ok((panic_hook, eyre_hook))
} }
} }

7
tests/install.rs Normal file
View File

@ -0,0 +1,7 @@
use color_eyre::install;
#[test]
fn double_install_should_not_panic() {
install().unwrap();
assert!(install().is_err());
}