Skip to content

Commit 10b944a

Browse files
authored
cli: add panic handler (#457)
basically let people know they should open a github issue!
1 parent 06ffcf0 commit 10b944a

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

crates/cli/src/main.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use config::Config;
1616
use log::info;
1717
use simplelog::CombinedLogger;
1818
use std::io;
19+
use std::panic;
1920
use std::path::PathBuf;
2021
use std::process::{self, ExitCode};
2122
use structopt::StructOpt;
@@ -140,6 +141,22 @@ struct Opt {
140141

141142
#[allow(clippy::too_many_lines)]
142143
fn main() -> Result<ExitCode> {
144+
let version = env!("CARGO_PKG_VERSION");
145+
146+
// via: https://github.com/astral-sh/ruff/blob/40fd52dde0ddf0b95a3433163f51560b4827ab75/crates/ruff_server/src/server.rs#L140
147+
panic::set_hook(Box::new(move |panic_info| {
148+
use std::io::Write;
149+
let backtrace = std::backtrace::Backtrace::force_capture();
150+
// Don't use `eprintln` because `eprintln` itself may panic if the pipe is broken.
151+
let mut stderr = std::io::stderr().lock();
152+
let open_an_issue = format!(
153+
r#"An internal error has occured with Squawk v{version}!
154+
Please open an issue at https://github.com/sbdchd/squawk/issues/new with the logs above!
155+
"#
156+
);
157+
writeln!(stderr, "{panic_info}\n{backtrace}\n{open_an_issue}").ok();
158+
}));
159+
143160
let opts = Opt::from_args();
144161

145162
if opts.verbose {

0 commit comments

Comments
 (0)