diff --git a/macro/src/wrapper.rs b/macro/src/wrapper.rs index 1be51cb..00e5db6 100644 --- a/macro/src/wrapper.rs +++ b/macro/src/wrapper.rs @@ -66,18 +66,9 @@ pub(crate) fn generate_jni_wrapper(attrs: TokenStream, input: TokenStream) -> Re quote::quote! { let ret = match result { Ok(x) => x, - Err(e) => match #env_iden.find_class(e.jclass()) { - Err(e) => panic!("error throwing Java exception -- failed resolving error class: {e}"), - Ok(class) => match #env_iden.new_string(format!("{e:?}")) { - Err(e) => panic!("error throwing Java exception -- failed creating error string: {e}"), - Ok(msg) => match #env_iden.new_object(class, "(Ljava/lang/String;)V", &[jni::objects::JValueGen::Object(&msg)]) { - Err(e) => panic!("error throwing Java exception -- failed creating object: {e}"), - Ok(obj) => match #env_iden.throw(jni::objects::JThrowable::from(obj)) { - Err(e) => panic!("error throwing Java exception -- failed throwing: {e}"), - Ok(_) => return #return_expr, - }, - }, - }, + Err(e) => match #env_iden.throw_new(e.jclass(), format!("{e:?}")) { + Err(e) => panic!("error throwing Java exception -- failed throwing: {e}"), + Ok(_) => return #return_expr } }; } diff --git a/src/test/Cargo.toml b/src/test/Cargo.toml index 2ab0e59..0327071 100644 --- a/src/test/Cargo.toml +++ b/src/test/Cargo.toml @@ -9,5 +9,7 @@ crate-type = ["cdylib"] path = "test.rs" [dependencies] +jni-toolbox-macro = { path = "../../macro/" } jni-toolbox = { path = "../.." } jni = "0.21" +thiserror = "1" diff --git a/src/test/java/toolbox/CustomException.java b/src/test/java/toolbox/CustomException.java new file mode 100644 index 0000000..5a0645a --- /dev/null +++ b/src/test/java/toolbox/CustomException.java @@ -0,0 +1,7 @@ +package toolbox; + +public class CustomException extends Exception { + public CustomException(String msg) { + super(msg); + } +} diff --git a/src/test/java/toolbox/Main.java b/src/test/java/toolbox/Main.java index f74f56b..e13176e 100644 --- a/src/test/java/toolbox/Main.java +++ b/src/test/java/toolbox/Main.java @@ -17,6 +17,7 @@ public class Main { static native boolean maybe(String optional); static native String optional(boolean present); static native String raw(); + static native void throw_error(); @Test public void argumentsByValue() { @@ -61,5 +62,9 @@ public class Main { assertNull(Main.optional(false)); assertEquals(Main.optional(true), "hello world!"); } - + + @Test + public void throwError() { + assertThrows(CustomException.class, Main::throw_error); + } } diff --git a/src/test/test.rs b/src/test/test.rs index 4780982..2fd568f 100644 --- a/src/test/test.rs +++ b/src/test/test.rs @@ -1,4 +1,4 @@ -use jni_toolbox::jni; +use jni_toolbox::{jni, JniToolboxError}; #[jni(package = "toolbox", class = "Main")] fn sum(a: i32, b: i32) -> i32 { @@ -33,3 +33,18 @@ fn optional(present: bool) -> Option { fn raw<'local>(env: &mut jni::JNIEnv<'local>) -> Result, jni::errors::Error> { env.new_string("hello world!") } + +#[derive(thiserror::Error, Debug)] +#[error("some test error")] +struct CustomError; + +impl JniToolboxError for CustomError { + fn jclass(&self) -> String { + "toolbox/CustomException".to_string() + } +} + +#[jni(package = "toolbox", class = "Main")] +fn throw_error() -> Result<(), CustomError> { + Err(CustomError) +}