Skip to content

Commit

Permalink
in debug, include location information in error message on panic
Browse files Browse the repository at this point in the history
  • Loading branch information
0x53A authored and Bromeon committed Nov 5, 2024
1 parent 387bd81 commit 2ac8d82
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
34 changes: 23 additions & 11 deletions godot-core/src/private.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down
14 changes: 11 additions & 3 deletions itest/rust/src/object_tests/dynamic_call_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down

0 comments on commit 2ac8d82

Please sign in to comment.