fix(jupyter-kernel): don't log errors from objects without a Symbol.for("Jupyter.display") (#20546)

Fast follow up to #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 <matthew@mastracci.com>
This commit is contained in:
Kyle Kelley 2023-09-18 09:31:20 -07:00 committed by GitHub
parent 701931477c
commit ee38bbbc8e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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)