From 5c9785057bafb76db08fa1474c91bdbcae864b74 Mon Sep 17 00:00:00 2001 From: Elina Date: Sun, 21 Jul 2024 16:57:08 +0500 Subject: [PATCH] make JsError::try_from not panic JsString::from paniced with non-string input. Now this it uses dyn_into so that if the passed JsValue input, when not an Error, can't be turned a string, a fallback message is provided --- crates/utils/src/errors.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/crates/utils/src/errors.rs b/crates/utils/src/errors.rs index 9d9eb51f..c93ee104 100644 --- a/crates/utils/src/errors.rs +++ b/crates/utils/src/errors.rs @@ -52,7 +52,13 @@ impl TryFrom for JsError { match value.dyn_into::() { Ok(error) => Ok(JsError::from(error)), Err(js_value) => { - let js_to_string = String::from(js_sys::JsString::from(js_value.clone())); + let js_to_string = js_value + .clone() + .dyn_into::() + .map(String::from) + .unwrap_or_else(|_| { + String::from("js_value passed has no string representation") + }); Err(NotJsError { js_value, js_to_string,