Skip to content

Commit 2ac8d82

Browse files
0x53ABromeon
authored andcommitted
in debug, include location information in error message on panic
1 parent 387bd81 commit 2ac8d82

File tree

2 files changed

+34
-14
lines changed

2 files changed

+34
-14
lines changed

godot-core/src/private.rs

+23-11
Original file line numberDiff line numberDiff line change
@@ -387,27 +387,39 @@ where
387387
// Handle panic info only in Debug mode.
388388
#[cfg(debug_assertions)]
389389
{
390-
let guard = info.lock().unwrap();
391-
let info = guard.as_ref().expect("no panic info available");
390+
let msg = extract_panic_message(err);
391+
let mut msg = format_panic_message(msg);
392+
393+
// try to add location information
394+
if let Ok(guard) = info.lock() {
395+
if let Some(info) = guard.as_ref() {
396+
msg = format!("{}\n at {}:{}", msg, info.file, info.line);
397+
}
398+
}
399+
392400
if print {
393401
godot_error!(
394-
"Rust function panicked at {}:{}.\n Context: {}",
395-
info.file,
396-
info.line,
402+
"Rust function panicked: {}\n Context: {}",
403+
msg,
397404
error_context()
398405
);
399406
//eprintln!("Backtrace:\n{}", info.backtrace);
400407
}
408+
409+
Err(msg)
401410
}
402411

403-
let msg = extract_panic_message(err);
404-
let msg = format_panic_message(msg);
412+
#[cfg(not(debug_assertions))]
413+
{
414+
let msg = extract_panic_message(err);
415+
let msg = format_panic_message(msg);
416+
417+
if print {
418+
godot_error!("{msg}");
419+
}
405420

406-
if print {
407-
godot_error!("{msg}");
421+
Err(msg)
408422
}
409-
410-
Err(msg)
411423
}
412424
}
413425
}

itest/rust/src/object_tests/dynamic_call_test.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,21 @@ fn dynamic_call_with_panic() {
148148

149149
assert_eq!(call_error.class_name(), Some("Object"));
150150
assert_eq!(call_error.method_name(), "call");
151-
assert_eq!(
152-
call_error.to_string(),
151+
152+
#[cfg(target_os = "windows")]
153+
let path = "itest\\rust\\src\\object_tests\\object_test.rs";
154+
#[cfg(not(target_os = "windows"))]
155+
let path = "itest/rust/src/object_tests/object_test.rs";
156+
157+
let expected_error_message = format!(
153158
"godot-rust function call failed: Object::call(&\"do_panic\")\
154159
\n Source: ObjPayload::do_panic()\
155-
\n Reason: [panic] do_panic exploded"
160+
\n Reason: [panic] do_panic exploded\
161+
\n at {path}:893"
156162
);
157163

164+
assert_eq!(call_error.to_string(), expected_error_message);
165+
158166
obj.free();
159167
}
160168

0 commit comments

Comments
 (0)