Skip to content

Commit

Permalink
fix(jupyter-kernel): don't log errors from objects without a `Symbol.…
Browse files Browse the repository at this point in the history
…for("Jupyter.display")` (denoland#20546)

Fast follow up to denoland#20537.

Before:


![image](https://github.com/denoland/deno/assets/836375/8a12e83d-9008-419b-bd1f-24c0ac90afd3)

After:

<img width="235" alt="image"
src="https://github.com/denoland/deno/assets/836375/467bf381-278e-4577-a980-7b0ddb08d2af">

---------

Co-authored-by: Matt Mastracci <[email protected]>
  • Loading branch information
rgbkrk and mmastrac authored Sep 18, 2023
1 parent 7019314 commit ee38bbb
Showing 1 changed file with 31 additions and 14 deletions.
45 changes: 31 additions & 14 deletions cli/tools/jupyter/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit ee38bbb

Please sign in to comment.