(vm: &VirtualMachine, py_err: &Py<PyBaseException>)
| 33 | } |
| 34 | |
| 35 | pub fn py_err_to_js_err(vm: &VirtualMachine, py_err: &Py<PyBaseException>) -> JsValue { |
| 36 | let js_err = vm.try_class("_js", "JSError").ok(); |
| 37 | let js_arg = if js_err.is_some_and(|js_err| py_err.fast_isinstance(&js_err)) { |
| 38 | py_err.get_arg(0) |
| 39 | } else { |
| 40 | None |
| 41 | }; |
| 42 | let js_arg = js_arg |
| 43 | .as_ref() |
| 44 | .and_then(|x| x.downcast_ref::<js_module::PyJsValue>()); |
| 45 | match js_arg { |
| 46 | Some(val) => val.value.clone(), |
| 47 | None => { |
| 48 | let res = |
| 49 | serde_wasm_bindgen::to_value(&exceptions::SerializeException::new(vm, py_err)); |
| 50 | match res { |
| 51 | Ok(err_info) => PyError::new(err_info).into(), |
| 52 | Err(_) => { |
| 53 | // Fallback: create a basic JS Error with the exception type and message |
| 54 | let exc_type = py_err.class().name().to_string(); |
| 55 | let msg = match py_err.as_object().str(vm) { |
| 56 | Ok(s) => format!("{exc_type}: {s}"), |
| 57 | Err(_) => exc_type, |
| 58 | }; |
| 59 | js_sys::Error::new(&msg).into() |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | pub fn js_py_typeerror(vm: &VirtualMachine, js_err: JsValue) -> PyBaseExceptionRef { |
| 67 | let msg: String = js_err.unchecked_into::<js_sys::Error>().to_string().into(); |
no test coverage detected