diff options
author | Kyle Kelley <rgbkrk@gmail.com> | 2023-09-18 09:31:20 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-09-18 16:31:20 +0000 |
commit | ee38bbbc8e716e0d91bf6558024240142d42f32c (patch) | |
tree | f0458116cd7bce0620e740945c372560ee323b60 /cli/tools/jupyter/server.rs | |
parent | 701931477c302c0fc9dbec529af5a16c198650aa (diff) |
fix(jupyter-kernel): don't log errors from objects without a `Symbol.for("Jupyter.display")` (#20546)
Fast follow up to #20537.
Before:

After:
<img width="235" alt="image"
src="https://github.com/denoland/deno/assets/836375/467bf381-278e-4577-a980-7b0ddb08d2af">
---------
Co-authored-by: Matt Mastracci <matthew@mastracci.com>
Diffstat (limited to 'cli/tools/jupyter/server.rs')
-rw-r--r-- | cli/tools/jupyter/server.rs | 45 |
1 files changed, 31 insertions, 14 deletions
diff --git a/cli/tools/jupyter/server.rs b/cli/tools/jupyter/server.rs index 2ed1a61d8..f0e523e1e 100644 --- a/cli/tools/jupyter/server.rs +++ b/cli/tools/jupyter/server.rs @@ -482,32 +482,49 @@ async fn get_jupyter_display( ) -> Result<Option<HashMap<String, serde_json::Value>>, AnyError> { let response = session .call_function_on_args( - r#"function (object) {{ - return JSON.stringify(object[Symbol.for("Jupyter.display")]()); - }}"# + r#"function (object) { + const display = object[Symbol.for("Jupyter.display")]; + if (typeof display === "function") { + return JSON.stringify(display()); + } else { + return null; + } + }"# .to_string(), &[evaluate_result.clone()], ) .await?; if let Some(exception_details) = &response.exception_details { - // TODO(rgbkrk): Return an error in userspace instead of Jupyter logs + // If the object doesn't have a Jupyter.display method or it throws an + // exception, we just ignore it and let the caller handle it. eprintln!("Exception encountered: {}", exception_details.text); return Ok(None); } - if let Some(serde_json::Value::String(json_str)) = response.result.value { - let data: HashMap<String, serde_json::Value> = - serde_json::from_str(&json_str)?; + match response.result.value { + Some(serde_json::Value::String(json_str)) => { + let Ok(data) = + serde_json::from_str::<HashMap<String, serde_json::Value>>(&json_str) + else { + eprintln!("Unexpected response from Jupyter.display: {json_str}"); + return Ok(None); + }; - if !data.is_empty() { - return Ok(Some(data)); + if !data.is_empty() { + return Ok(Some(data)); + } + } + Some(serde_json::Value::Null) => { + // Object did not have the Jupyter display spec + return Ok(None); + } + _ => { + eprintln!( + "Unexpected response from Jupyter.display: {:?}", + response.result + ) } - } else { - eprintln!( - "Unexpected response from Jupyter.display: {:?}", - response.result.clone().value - ); } Ok(None) |