summaryrefslogtreecommitdiff
path: root/cli
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2023-12-18 11:43:02 +0100
committerGitHub <noreply@github.com>2023-12-18 11:43:02 +0100
commita44a5de43013becb91e4725e508051e62f628467 (patch)
treef12a4135816cea35f4853ff2730a0df2275091e5 /cli
parent9ede8d7b69c1d2859bb03e5bdffd7407042914ec (diff)
refactor: factor out cdp::ExceptionThrown notification (#21623)
Just removing some duplicated code.
Diffstat (limited to 'cli')
-rw-r--r--cli/cdp.rs24
-rw-r--r--cli/tests/integration/inspector_tests.rs9
-rw-r--r--cli/tools/repl/mod.rs16
-rw-r--r--cli/tools/run/hmr.rs21
4 files changed, 40 insertions, 30 deletions
diff --git a/cli/cdp.rs b/cli/cdp.rs
index cb9933a03..6c2adc552 100644
--- a/cli/cdp.rs
+++ b/cli/cdp.rs
@@ -322,6 +322,17 @@ pub struct ExceptionDetails {
pub exception_meta_data: Option<serde_json::Map<String, Value>>,
}
+impl ExceptionDetails {
+ pub fn get_message_and_description(&self) -> (String, String) {
+ let description = self
+ .exception
+ .clone()
+ .and_then(|ex| ex.description)
+ .unwrap_or_else(|| "undefined".to_string());
+ (self.text.to_string(), description)
+ }
+}
+
/// <https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#type-StackTrace>
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
@@ -502,3 +513,16 @@ pub struct TakePreciseCoverageResponse {
pub result: Vec<ScriptCoverage>,
pub timestamp: f64,
}
+
+#[derive(Debug, Deserialize)]
+pub struct Notification {
+ pub method: String,
+ pub params: Value,
+}
+/// <https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#event-exceptionThrown>
+#[derive(Debug, Deserialize)]
+#[serde(rename_all = "camelCase")]
+pub struct ExceptionThrown {
+ pub timestamp: f64,
+ pub exception_details: ExceptionDetails,
+}
diff --git a/cli/tests/integration/inspector_tests.rs b/cli/tests/integration/inspector_tests.rs
index 41c126cdb..95310667b 100644
--- a/cli/tests/integration/inspector_tests.rs
+++ b/cli/tests/integration/inspector_tests.rs
@@ -1366,12 +1366,11 @@ async fn inspector_error_with_npm_import() {
.send(json!({"id":4,"method":"Debugger.resume"}))
.await;
tester
- .assert_received_messages(&[r#"{"id":4,"result":{}}"#], &[])
+ .assert_received_messages(
+ &[r#"{"id":4,"result":{}}"#],
+ &[r#"{"method":"Runtime.exceptionThrown","#],
+ )
.await;
-
- // TODO(bartlomieju): this is a partial fix, we should assert that
- // "Runtime.exceptionThrown" notification was sent, but a bindings for this
- // notification is not yet there
assert_eq!(&tester.stderr_line(), "Debugger session started.");
assert_eq!(&tester.stderr_line(), "error: Uncaught Error: boom!");
diff --git a/cli/tools/repl/mod.rs b/cli/tools/repl/mod.rs
index 3700911d6..4b7453cb6 100644
--- a/cli/tools/repl/mod.rs
+++ b/cli/tools/repl/mod.rs
@@ -3,11 +3,13 @@
use crate::args::CliOptions;
use crate::args::Flags;
use crate::args::ReplFlags;
+use crate::cdp;
use crate::colors;
use crate::factory::CliFactory;
use crate::file_fetcher::FileFetcher;
use deno_core::error::AnyError;
use deno_core::futures::StreamExt;
+use deno_core::serde_json;
use deno_core::unsync::spawn_blocking;
use deno_runtime::permissions::Permissions;
use deno_runtime::permissions::PermissionsContainer;
@@ -69,14 +71,12 @@ async fn read_line_and_poll(
}
message = notifications.next() => {
if let Some(message) = message {
- let method = message.get("method").unwrap().as_str().unwrap();
- if method == "Runtime.exceptionThrown" {
- let params = message.get("params").unwrap().as_object().unwrap();
- let exception_details = params.get("exceptionDetails").unwrap().as_object().unwrap();
- let text = exception_details.get("text").unwrap().as_str().unwrap();
- let exception = exception_details.get("exception").unwrap().as_object().unwrap();
- let description = exception.get("description").and_then(|d| d.as_str()).unwrap_or("undefined");
- println!("{text} {description}");
+ let notification: cdp::Notification = serde_json::from_value(message).unwrap();
+ eprintln!("notification {:#?}", notification);
+ if notification.method == "Runtime.exceptionThrown" {
+ let exception_thrown: cdp::ExceptionThrown = serde_json::from_value(notification.params).unwrap();
+ let (message, description) = exception_thrown.exception_details.get_message_and_description();
+ println!("{} {}", message, description);
}
}
}
diff --git a/cli/tools/run/hmr.rs b/cli/tools/run/hmr.rs
index fb6651fed..0842c0084 100644
--- a/cli/tools/run/hmr.rs
+++ b/cli/tools/run/hmr.rs
@@ -8,24 +8,15 @@ use deno_core::error::generic_error;
use deno_core::error::AnyError;
use deno_core::futures::StreamExt;
use deno_core::serde_json::json;
-use deno_core::serde_json::Value;
use deno_core::serde_json::{self};
use deno_core::url::Url;
use deno_core::LocalInspectorSession;
use deno_runtime::colors;
-use serde::Deserialize;
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::Arc;
use tokio::select;
-// TODO(bartlomieju): the same thing is used in the REPL. Deduplicate.
-#[derive(Debug, Deserialize)]
-pub struct RpcNotification {
- pub method: String,
- pub params: Value,
-}
-
fn explain(status: &cdp::Status) -> &'static str {
match status {
cdp::Status::Ok => "OK",
@@ -177,15 +168,11 @@ impl HmrRunner {
select! {
biased;
Some(notification) = session_rx.next() => {
- let notification = serde_json::from_value::<RpcNotification>(notification)?;
- // TODO(bartlomieju): this is not great... and the code is duplicated with the REPL.
+ let notification = serde_json::from_value::<cdp::Notification>(notification)?;
if notification.method == "Runtime.exceptionThrown" {
- let params = notification.params;
- let exception_details = params.get("exceptionDetails").unwrap().as_object().unwrap();
- let text = exception_details.get("text").unwrap().as_str().unwrap();
- let exception = exception_details.get("exception").unwrap().as_object().unwrap();
- let description = exception.get("description").and_then(|d| d.as_str()).unwrap_or("undefined");
- break Err(generic_error(format!("{text} {description}")));
+ let exception_thrown = serde_json::from_value::<cdp::ExceptionThrown>(notification.params)?;
+ let (message, description) = exception_thrown.exception_details.get_message_and_description();
+ break Err(generic_error(format!("{} {}", message, description)));
} else if notification.method == "Debugger.scriptParsed" {
let params = serde_json::from_value::<cdp::ScriptParsed>(notification.params)?;
if params.url.starts_with("file://") {