Skip to content

Commit 0ddc593

Browse files
committed
feat: better custom exception handling + related test
1 parent 7e2c8d4 commit 0ddc593

File tree

5 files changed

+34
-14
lines changed

5 files changed

+34
-14
lines changed

macro/src/wrapper.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,9 @@ pub(crate) fn generate_jni_wrapper(attrs: TokenStream, input: TokenStream) -> Re
6666
quote::quote! {
6767
let ret = match result {
6868
Ok(x) => x,
69-
Err(e) => match #env_iden.find_class(e.jclass()) {
70-
Err(e) => panic!("error throwing Java exception -- failed resolving error class: {e}"),
71-
Ok(class) => match #env_iden.new_string(format!("{e:?}")) {
72-
Err(e) => panic!("error throwing Java exception -- failed creating error string: {e}"),
73-
Ok(msg) => match #env_iden.new_object(class, "(Ljava/lang/String;)V", &[jni::objects::JValueGen::Object(&msg)]) {
74-
Err(e) => panic!("error throwing Java exception -- failed creating object: {e}"),
75-
Ok(obj) => match #env_iden.throw(jni::objects::JThrowable::from(obj)) {
76-
Err(e) => panic!("error throwing Java exception -- failed throwing: {e}"),
77-
Ok(_) => return #return_expr,
78-
},
79-
},
80-
},
69+
Err(e) => match #env_iden.throw_new(e.jclass(), format!("{e:?}")) {
70+
Err(e) => panic!("error throwing Java exception -- failed throwing: {e}"),
71+
Ok(_) => return #return_expr
8172
}
8273
};
8374
}

src/test/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,7 @@ crate-type = ["cdylib"]
99
path = "test.rs"
1010

1111
[dependencies]
12+
jni-toolbox-macro = { path = "../../macro/" }
1213
jni-toolbox = { path = "../.." }
1314
jni = "0.21"
15+
thiserror = "1"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package toolbox;
2+
3+
public class CustomException extends Exception {
4+
public CustomException(String msg) {
5+
super(msg);
6+
}
7+
}

src/test/java/toolbox/Main.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class Main {
1717
static native boolean maybe(String optional);
1818
static native String optional(boolean present);
1919
static native String raw();
20+
static native void throw_error();
2021

2122
@Test
2223
public void argumentsByValue() {
@@ -61,5 +62,9 @@ public void nullableReturn() {
6162
assertNull(Main.optional(false));
6263
assertEquals(Main.optional(true), "hello world!");
6364
}
64-
65+
66+
@Test
67+
public void throwError() {
68+
assertThrows(CustomException.class, Main::throw_error);
69+
}
6570
}

src/test/test.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use jni_toolbox::jni;
1+
use jni_toolbox::{jni, JniToolboxError};
22

33
#[jni(package = "toolbox", class = "Main")]
44
fn sum(a: i32, b: i32) -> i32 {
@@ -33,3 +33,18 @@ fn optional(present: bool) -> Option<String> {
3333
fn raw<'local>(env: &mut jni::JNIEnv<'local>) -> Result<jni::objects::JString<'local>, jni::errors::Error> {
3434
env.new_string("hello world!")
3535
}
36+
37+
#[derive(thiserror::Error, Debug)]
38+
#[error("some test error")]
39+
struct CustomError;
40+
41+
impl JniToolboxError for CustomError {
42+
fn jclass(&self) -> String {
43+
"toolbox/CustomException".to_string()
44+
}
45+
}
46+
47+
#[jni(package = "toolbox", class = "Main")]
48+
fn throw_error() -> Result<(), CustomError> {
49+
Err(CustomError)
50+
}

0 commit comments

Comments
 (0)