diff options
| author | Nayeem Rahman <nayeemrmn99@gmail.com> | 2023-04-27 22:36:49 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-04-27 23:36:49 +0200 |
| commit | 504482dadd4d8cd9e4105d56ed86802906767f39 (patch) | |
| tree | acacb80e50ccfae578822dda63a3ec9e61108d60 /cli | |
| parent | 6cd62ea5e969de258b1d308daf5bec91e73e79d3 (diff) | |
fix(repl): print unhandled rejections and event errors (#18878)
Fixes #8858.
Fixes #8869.
```
$ target/debug/deno
Deno 1.32.5
exit using ctrl+d, ctrl+c, or close()
REPL is running with all permissions allowed.
To specify permissions, run `deno repl` with allow flags.
> Promise.reject(new Error("bar"));
Promise { <rejected> Error: bar
at <anonymous>:2:16 }
Uncaught (in promise) Error: bar
at <anonymous>:2:16
> reportError(new Error("baz"));
undefined
Uncaught Error: baz
at <anonymous>:2:13
>
Diffstat (limited to 'cli')
| -rw-r--r-- | cli/tests/integration/repl_tests.rs | 30 | ||||
| -rw-r--r-- | cli/tools/repl/mod.rs | 19 | ||||
| -rw-r--r-- | cli/tools/repl/session.rs | 9 |
3 files changed, 51 insertions, 7 deletions
diff --git a/cli/tests/integration/repl_tests.rs b/cli/tests/integration/repl_tests.rs index a473dc200..d9966fe8f 100644 --- a/cli/tests/integration/repl_tests.rs +++ b/cli/tests/integration/repl_tests.rs @@ -784,13 +784,41 @@ fn pty_tab_handler() { } #[test] +fn repl_error() { + util::with_pty(&["repl"], |mut console| { + console.write_line("console.log(1);"); + console.expect_all(&["1", "undefined"]); + console.write_line(r#"throw new Error("foo");"#); + console.expect("Uncaught Error: foo"); + console.expect(" at <anonymous>"); + console.write_line("console.log(2);"); + console.expect("2"); + }); +} + +#[test] +fn repl_reject() { + util::with_pty(&["repl"], |mut console| { + console.write_line("console.log(1);"); + console.expect_all(&["1", "undefined"]); + console.write_line(r#"Promise.reject(new Error("foo"));"#); + console.expect("Promise { <rejected> Error: foo"); + console.expect("Uncaught (in promise) Error: foo"); + console.expect(" at <anonymous>"); + console.write_line("console.log(2);"); + console.expect("2"); + }); +} + +#[test] fn repl_report_error() { util::with_pty(&["repl"], |mut console| { console.write_line("console.log(1);"); console.expect_all(&["1", "undefined"]); - // TODO(nayeemrmn): The REPL should report event errors and rejections. console.write_line(r#"reportError(new Error("foo"));"#); console.expect("undefined"); + console.expect("Uncaught Error: foo"); + console.expect(" at <anonymous>"); console.write_line("console.log(2);"); console.expect("2"); }); diff --git a/cli/tools/repl/mod.rs b/cli/tools/repl/mod.rs index bfba62752..0a6d9b9e9 100644 --- a/cli/tools/repl/mod.rs +++ b/cli/tools/repl/mod.rs @@ -7,6 +7,7 @@ use crate::colors; use crate::file_fetcher::FileFetcher; use crate::proc_state::ProcState; use deno_core::error::AnyError; +use deno_core::futures::StreamExt; use deno_runtime::permissions::Permissions; use deno_runtime::permissions::PermissionsContainer; use rustyline::error::ReadlineError; @@ -30,8 +31,11 @@ async fn read_line_and_poll( message_handler: &mut RustylineSyncMessageHandler, editor: ReplEditor, ) -> Result<String, ReadlineError> { + #![allow(clippy::await_holding_refcell_ref)] let mut line_fut = tokio::task::spawn_blocking(move || editor.readline()); let mut poll_worker = true; + let notifications_rc = repl_session.notifications.clone(); + let mut notifications = notifications_rc.borrow_mut(); loop { tokio::select! { @@ -57,7 +61,20 @@ async fn read_line_and_poll( } poll_worker = true; - }, + } + 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").unwrap().as_str().unwrap(); + println!("{text} {description}"); + } + } + } _ = repl_session.run_event_loop(), if poll_worker => { poll_worker = false; } diff --git a/cli/tools/repl/session.rs b/cli/tools/repl/session.rs index b2645097c..6f8db6fcd 100644 --- a/cli/tools/repl/session.rs +++ b/cli/tools/repl/session.rs @@ -1,5 +1,7 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. +use std::cell::RefCell; +use std::rc::Rc; use std::sync::Arc; use crate::args::CliOptions; @@ -128,12 +130,9 @@ pub struct ReplSession { session: LocalInspectorSession, pub context_id: u64, pub language_server: ReplLanguageServer, + pub notifications: Rc<RefCell<UnboundedReceiver<Value>>>, has_initialized_node_runtime: bool, referrer: ModuleSpecifier, - // FIXME(bartlomieju): this field should be used to listen - // for "exceptionThrown" notifications - #[allow(dead_code)] - notification_rx: UnboundedReceiver<Value>, } impl ReplSession { @@ -193,7 +192,7 @@ impl ReplSession { language_server, has_initialized_node_runtime: false, referrer, - notification_rx, + notifications: Rc::new(RefCell::new(notification_rx)), }; // inject prelude |
