MCPcopy Index your code
hub / github.com/RustPython/RustPython / py_err_to_js_err

Function py_err_to_js_err

crates/wasm/src/convert.rs:35–64  ·  view source on GitHub ↗
(vm: &VirtualMachine, py_err: &Py<PyBaseException>)

Source from the content-addressed store, hash-verified

33}
34
35pub 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
66pub fn js_py_typeerror(vm: &VirtualMachine, js_err: JsValue) -> PyBaseExceptionRef {
67 let msg: String = js_err.unchecked_into::<js_sys::Error>().to_string().into();

Callers 4

as_jsMethod · 0.85
pyresult_to_js_resultFunction · 0.85
into_jsMethod · 0.85
load_moduleFunction · 0.85

Calls 12

newFunction · 0.85
okMethod · 0.80
try_classMethod · 0.80
fast_isinstanceMethod · 0.80
to_stringMethod · 0.80
get_argMethod · 0.45
as_refMethod · 0.45
cloneMethod · 0.45
nameMethod · 0.45
classMethod · 0.45
strMethod · 0.45
as_objectMethod · 0.45

Tested by

no test coverage detected