Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add better jni error reporting inside the godot editor #627

Merged
merged 9 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package godot.tests.exception

import godot.Node
import godot.annotation.RegisterClass
import godot.annotation.RegisterFunction

@RegisterClass
class ExceptionTest: Node() {

@RegisterFunction
fun throwException() {
throw RuntimeException("Test exception")
}
}
8 changes: 8 additions & 0 deletions harness/tests/test/unit/test_exception.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
extends "res://addons/gut/test.gd"


func test_exception_printing_in_editor() -> void:
var exception_test_script = load("res://scripts/godot/tests/exception/ExceptionTest.gdj").new()
exception_test_script.throw_exception()
assert_true(true) # fake assert; exception printed to console and godot editor
exception_test_script.free()
36 changes: 33 additions & 3 deletions src/jni/env.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,34 @@ namespace jni {
return env->ExceptionCheck();
}

void Env::exception_describe() {
String Env::exception_describe() {
#ifdef DEBUG_ENABLED
if (jthrowable e = env->ExceptionOccurred()) {
jclass string_writer_class {env->FindClass("java/io/StringWriter")};
jmethodID string_writer_constructor {env->GetMethodID(string_writer_class, "<init>", "()V")};
jobject string_writer {env->NewObject(string_writer_class, string_writer_constructor)};

jclass print_writer_class {env->FindClass("java/io/PrintWriter")};
jmethodID print_writer_constructor {env->GetMethodID(print_writer_class, "<init>", "(Ljava/io/Writer;)V")};
jobject print_writer {env->NewObject(print_writer_class, print_writer_constructor, string_writer)};

jclass throwable_class {env->FindClass("java/lang/Throwable")};
jmethodID print_stack_trace_method {env->GetMethodID(throwable_class, "printStackTrace", "(Ljava/io/PrintWriter;)V")};
env->CallVoidMethod(e, print_stack_trace_method, print_writer);

jmethodID to_string_method {env->GetMethodID(string_writer_class, "toString", "()Ljava/lang/String;")};
auto j_stack_trace_string {(jstring) env->CallObjectMethod(string_writer, to_string_method)};

const char* c_stack_trace {env->GetStringUTFChars(j_stack_trace_string, nullptr)};
String stack_trace {c_stack_trace};
env->ReleaseStringUTFChars(j_stack_trace_string, c_stack_trace);

return stack_trace;
}
#else
env->ExceptionDescribe();
#endif
return {};
}

void Env::exception_clear() {
Expand All @@ -48,9 +74,13 @@ namespace jni {

void Env::check_exceptions() {
if (exception_check()) {
exception_describe();
String exception {exception_describe()};
exception_clear();
HANDLE_JVM_EXCEPTIONS(true, "An exception has occurred!");

if (exception.is_empty()) {
exception = "An exception has occurred!";
}
HANDLE_JVM_EXCEPTIONS(true, exception);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/jni/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace jni {
String from_jstring(jni::JString str);

bool exception_check();
void exception_describe();
String exception_describe();
void exception_clear();

void check_exceptions();
Expand Down
Loading