diff --git a/godot-core/src/private.rs b/godot-core/src/private.rs index 543c2b943..3a81d232e 100644 --- a/godot-core/src/private.rs +++ b/godot-core/src/private.rs @@ -387,27 +387,39 @@ where // Handle panic info only in Debug mode. #[cfg(debug_assertions)] { - let guard = info.lock().unwrap(); - let info = guard.as_ref().expect("no panic info available"); + let msg = extract_panic_message(err); + let mut msg = format_panic_message(msg); + + // try to add location information + if let Ok(guard) = info.lock() { + if let Some(info) = guard.as_ref() { + msg = format!("{}\n at {}:{}", msg, info.file, info.line); + } + } + if print { godot_error!( - "Rust function panicked at {}:{}.\n Context: {}", - info.file, - info.line, + "Rust function panicked: {}\n Context: {}", + msg, error_context() ); //eprintln!("Backtrace:\n{}", info.backtrace); } + + Err(msg) } - let msg = extract_panic_message(err); - let msg = format_panic_message(msg); + #[cfg(not(debug_assertions))] + { + let msg = extract_panic_message(err); + let msg = format_panic_message(msg); + + if print { + godot_error!("{msg}"); + } - if print { - godot_error!("{msg}"); + Err(msg) } - - Err(msg) } } } diff --git a/itest/rust/src/object_tests/dynamic_call_test.rs b/itest/rust/src/object_tests/dynamic_call_test.rs index 65a38a0f8..277a4912a 100644 --- a/itest/rust/src/object_tests/dynamic_call_test.rs +++ b/itest/rust/src/object_tests/dynamic_call_test.rs @@ -148,13 +148,21 @@ fn dynamic_call_with_panic() { assert_eq!(call_error.class_name(), Some("Object")); assert_eq!(call_error.method_name(), "call"); - assert_eq!( - call_error.to_string(), + + #[cfg(target_os = "windows")] + let path = "itest\\rust\\src\\object_tests\\object_test.rs"; + #[cfg(not(target_os = "windows"))] + let path = "itest/rust/src/object_tests/object_test.rs"; + + let expected_error_message = format!( "godot-rust function call failed: Object::call(&\"do_panic\")\ \n Source: ObjPayload::do_panic()\ - \n Reason: [panic] do_panic exploded" + \n Reason: [panic] do_panic exploded\ + \n at {path}:893" ); + assert_eq!(call_error.to_string(), expected_error_message); + obj.free(); }