feat(rome_cli): add a user-friendly panic hook to the CLI
MODIFY
crates/rome_cli/src/bin/rome.rs
crates/rome_cli/src/bin/rome.rs
-use rome_cli::{run_cli, CliSession, Termination};
+use rome_cli::{run_cli, setup_panic_handler, CliSession, Termination};
///
/// To run this example, run:
@@ -20,5 +20,6 @@ use rome_cli::{run_cli, CliSession, Termination};
/// ```
///
fn main() -> Result<(), Termination> {
+ setup_panic_handler();
run_cli(CliSession::from_env())
}

MODIFY
crates/rome_cli/src/lib.rs
crates/rome_cli/src/lib.rs
@@ -5,6 +5,9 @@ use rome_core::App;
mod commands;
mod metrics;
+mod panic;
+
+pub use panic::setup_panic_handler;
/// Global context for an execution of the CLI
pub struct CliSession {

ADD
crates/rome_cli/src/panic.rs
+use std::{
+ fmt::Write,
+ panic::{set_hook, PanicInfo},
+ thread,
+};
+
+/// Installs a global panic handler to show a user-friendly error message
+/// in case the CLI panics
+pub fn setup_panic_handler() {
+ set_hook(Box::new(panic_handler))
+}
+
+fn panic_handler(info: &PanicInfo) {
+ // Buffer the error message to a string before printing it to stderr at once
+ // to prevent it from getting mixed with other errors if multiple threads
+ // panic at the same time
+ let mut error = String::new();
+
+ writeln!(error, "Rome encountered an unexpected error").unwrap();
+ writeln!(error).unwrap();
+
+ writeln!(error, "This is a bug in Rome, not an error in your code, and we would appreciate it if you could report it to https://github.com/rome/tools/issues/ along with the following information to help us fixing the issue:").unwrap();
+ writeln!(error).unwrap();
+
+ if let Some(location) = info.location() {
+ writeln!(error, "Source Location: {location}").unwrap();
+ }
+
+ if let Some(thread) = thread::current().name() {
+ writeln!(error, "Thread Name: {thread}").unwrap();
+ }
+
+ let payload = info.payload();
+ if let Some(msg) = payload.downcast_ref::<&'static str>() {
+ writeln!(error, "Message: {msg}").unwrap();
+ } else if let Some(msg) = payload.downcast_ref::<String>() {
+ writeln!(error, "Message: {msg}").unwrap();
+ }
+
+ eprintln!("{error}");
+}

feat(rome_cli): add a user-friendly panic hook to the CLI (#2299)