From d9c884da65a00996d5fbbd9103ab63e3dc808377 Mon Sep 17 00:00:00 2001 From: Elias Rhouzlane Date: Thu, 9 Jan 2025 09:08:06 +0100 Subject: [PATCH] feat(cli/hmr): print compile error with exception details --- cli/cdp.rs | 2 ++ cli/tools/run/hmr.rs | 28 +++++++++++++++++++++------- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/cli/cdp.rs b/cli/cdp.rs index df82d58d9f..14113a9889 100644 --- a/cli/cdp.rs +++ b/cli/cdp.rs @@ -291,8 +291,10 @@ pub type UnserializableValue = String; /// #[derive(Debug, Deserialize)] +#[serde(rename_all = "camelCase")] pub struct SetScriptSourceResponse { pub status: Status, + pub exception_details: Option, } #[derive(Debug, Deserialize)] diff --git a/cli/tools/run/hmr.rs b/cli/tools/run/hmr.rs index 913e119689..d48ce4e8f9 100644 --- a/cli/tools/run/hmr.rs +++ b/cli/tools/run/hmr.rs @@ -19,14 +19,28 @@ use crate::emit::Emitter; use crate::util::file_watcher::WatcherCommunicator; use crate::util::file_watcher::WatcherRestartMode; -fn explain(status: &cdp::Status) -> &'static str { +fn explain( + status: &cdp::Status, + exception_details: &Option, +) -> String { match status { - cdp::Status::Ok => "OK", - cdp::Status::CompileError => "compile error", - cdp::Status::BlockedByActiveGenerator => "blocked by active generator", - cdp::Status::BlockedByActiveFunction => "blocked by active function", + cdp::Status::Ok => "OK".to_string(), + cdp::Status::CompileError => { + if let Some(details) = exception_details { + let (message, description) = details.get_message_and_description(); + format!("compile error: {} - {}", message, description) + } else { + "compile error: No exception details available".to_string() + } + } + cdp::Status::BlockedByActiveGenerator => { + "blocked by active generator".to_string() + } + cdp::Status::BlockedByActiveFunction => { + "blocked by active function".to_string() + } cdp::Status::BlockedByTopLevelEsModuleChange => { - "blocked by top-level ES module change" + "blocked by top-level ES module change".to_string() } } } @@ -154,7 +168,7 @@ impl crate::worker::HmrRunner for HmrRunner { break; } - self.watcher_communicator.print(format!("Failed to reload module {}: {}.", module_url, colors::gray(explain(&result.status)))); + self.watcher_communicator.print(format!("Failed to reload module {}: {}.", module_url, colors::gray(&explain(&result.status, &result.exception_details)))); if should_retry(&result.status) && tries <= 2 { tries += 1; tokio::time::sleep(std::time::Duration::from_millis(100)).await;