feat: add a panic hook, present issue link

This commit is contained in:
rvcas 2023-06-13 20:15:38 -04:00 committed by Lucas
parent b79437f596
commit 44e066f907
2 changed files with 40 additions and 2 deletions

View File

@ -4,7 +4,7 @@
### Added
N/A
- **aiken**: added panic hook to present a link to make a bug report
### Fixed

View File

@ -8,6 +8,7 @@ use aiken::{
},
};
use clap::Parser;
use owo_colors::OwoColorize;
/// Aiken: a smart-contract language and toolchain for Cardano
#[derive(Parser)]
@ -45,7 +46,8 @@ impl Default for Cmd {
}
fn main() -> miette::Result<()> {
miette::set_panic_hook();
panic_handler();
match Cmd::default() {
Cmd::New(args) => new::exec(args),
Cmd::Fmt(args) => fmt::exec(args),
@ -72,3 +74,39 @@ fn version() -> String {
built_info::GIT_COMMIT_HASH_SHORT.unwrap_or(&nix_git_rev)
)
}
fn panic_handler() {
std::panic::set_hook(Box::new(move |info| {
let message = info
.payload()
.downcast_ref::<&str>()
.map(|s| (*s).to_string())
.or_else(|| {
info.payload()
.downcast_ref::<String>()
.map(|s| s.to_string())
})
.unwrap_or_else(|| "unknown error".to_string());
let location = info.location().map_or_else(
|| "".into(),
|location| format!("{}:{}\n\n ", location.file(), location.line()),
);
let error_message = indoc::formatdoc! {
r#"{fatal}
Whoops! You found a bug in the Aiken compiler.
Please report this error at https://github.com/aiken-lang/aiken/issues/new.
In your bug report please provide the information below and if possible the code
that produced it.
{location}{message}"#,
fatal = "aiken::fatal::error".red().bold(),
location = location.purple(),
};
println!("{error_message}")
}));
}