Skip to content

Commit

Permalink
make JsError::try_from not panic
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ranile committed Jul 21, 2024
1 parent 1043eaf commit 5c97850
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion crates/utils/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,13 @@ impl TryFrom<JsValue> for JsError {
match value.dyn_into::<js_sys::Error>() {
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::<js_sys::JsString>()
.map(String::from)
.unwrap_or_else(|_| {
String::from("js_value passed has no string representation")
});
Err(NotJsError {
js_value,
js_to_string,
Expand Down

0 comments on commit 5c97850

Please sign in to comment.