mirror of
https://github.com/hexedtech/jni-toolbox.git
synced 2024-12-22 13:24:53 +01:00
feat: better custom exception handling + related test
This commit is contained in:
parent
7e2c8d4138
commit
0ddc593771
5 changed files with 34 additions and 14 deletions
|
@ -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
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
@ -9,5 +9,7 @@ crate-type = ["cdylib"]
|
|||
path = "test.rs"
|
||||
|
||||
[dependencies]
|
||||
jni-toolbox-macro = { path = "../../macro/" }
|
||||
jni-toolbox = { path = "../.." }
|
||||
jni = "0.21"
|
||||
thiserror = "1"
|
||||
|
|
7
src/test/java/toolbox/CustomException.java
Normal file
7
src/test/java/toolbox/CustomException.java
Normal file
|
@ -0,0 +1,7 @@
|
|||
package toolbox;
|
||||
|
||||
public class CustomException extends Exception {
|
||||
public CustomException(String msg) {
|
||||
super(msg);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<String> {
|
|||
fn raw<'local>(env: &mut jni::JNIEnv<'local>) -> Result<jni::objects::JString<'local>, 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)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue