use std::panic::PanicInfo;
use colored::Colorize;
use supports_hyperlinks::Stream;
use terminal_link::Link;
use url::Url;
use crate::repository::validate::validate_repository;
#[derive(Debug)]
pub struct CargoPanicMetadata {
pub repository: Option<String>,
pub version: String,
pub pkg_name: String,
pub crate_name: String,
}
pub fn append_panic_handler<F>(hook_fn: F, metadata: CargoPanicMetadata)
where
F: Fn(&PanicInfo<'_>, &CargoPanicMetadata) + Sync + Send + 'static,
{
let prev_hook = std::panic::take_hook();
std::panic::set_hook(Box::new(move |panic_info| {
prev_hook(panic_info);
hook_fn(panic_info, &metadata);
}));
}
pub fn ph_suggest_issue_tracker(info: &PanicInfo<'_>, metadata: &CargoPanicMetadata) {
if cfg!(not(feature = "color")) {
colored::control::set_override(false);
}
let mut report_url = None;
if let Some(repo) = &metadata.repository {
if !repo.is_empty() {
if let Some(provider) = validate_repository(&Url::parse(repo).unwrap()) {
report_url = Some(provider.build_issue_url(info, metadata));
}
}
}
println!(
"{}",
format!(
"\n---------------------- {} ----------------------\n",
"Crash Detected".bold()
)
.red()
);
if let Some(url) = report_url {
println!(
"{}",
"This application has issue tracker support enabled.".italic()
);
println!(
"{}",
"Click the link below to report this crash to the developers.".bold()
);
if supports_hyperlinks::on(Stream::Stdout)
&& !std::env::var("TERM_PROGRAM")
.unwrap_or("unknown".to_string())
.contains("vscode")
{
let link = Link::new("Report Crash", url.as_str());
println!("\n[{}]", link.to_string().cyan().bold());
} else {
println!("\n{}", url.to_string().cyan().bold());
}
} else {
if cfg!(debug_assertions) {
println!(
"{}",
"This application has issue tracker support enabled.".italic()
);
println!("However, it was not possible to determine the repository URL.");
println!(
"Please add a `{}` key to your {}.",
"repository".bright_green().bold(),
"Cargo.toml".cyan().bold()
);
println!(
"{}",
"\nThere is also a chance your repository service is not supported\nYou can request support at: https://github.com/ewpratten/crashreport-rs".italic()
);
} else {
println!(
"{}",
"This application has issue tracker support enabled.".italic()
);
println!("However, it was not possible to determine the issue tracker URL.");
}
}
println!(
"{}",
"\n------------------------------------------------------------\n".red()
)
}