diff --git a/java/src/errors.rs b/java/src/errors.rs index 618fc26b5f..031a898551 100644 --- a/java/src/errors.rs +++ b/java/src/errors.rs @@ -62,7 +62,7 @@ pub fn handle_errors(env: &mut JNIEnv, result: Result) -> Option error => throw_java_exception(env, ExceptionType::Exception, &error.to_string()), }; // Return `None` because we need to still return a value after throwing. - // This signals to `handle_panics` that we need to return the default value. + // This signals to the caller that we need to return the default value. None } } @@ -70,20 +70,17 @@ pub fn handle_errors(env: &mut JNIEnv, result: Result) -> Option // This function handles Rust panics by converting them into Java exceptions and throwing them. // `func` returns an `Option` because this is intended to wrap the output of `handle_errors`. -// When an exception is thrown, a value still need to be returned, so a `default_value` is used for this. pub fn handle_panics Option>( func: F, ffi_func_name: &str, - default_value: T, -) -> T { +) -> Option { match std::panic::catch_unwind(func) { - Ok(Some(value)) => value, - Ok(None) => default_value, + Ok(value) => value, Err(_err) => { // Following https://github.com/jni-rs/jni-rs/issues/76#issuecomment-363523906 // and throwing a runtime exception is not feasible here because of https://github.com/jni-rs/jni-rs/issues/432 error!("Native function {} panicked.", ffi_func_name); - default_value + None } } } diff --git a/java/src/ffi_test.rs b/java/src/ffi_test.rs index 862cfdf1d3..c0b71afe06 100644 --- a/java/src/ffi_test.rs +++ b/java/src/ffi_test.rs @@ -168,8 +168,8 @@ pub extern "system" fn Java_glide_ffi_FfiTest_handlePanics<'local>( } }, "handlePanics", - default_value, ) + .unwrap_or(default_value) } #[no_mangle] diff --git a/java/src/lib.rs b/java/src/lib.rs index def7584950..cf5bdee5aa 100644 --- a/java/src/lib.rs +++ b/java/src/lib.rs @@ -110,8 +110,8 @@ pub extern "system" fn Java_glide_ffi_resolvers_RedisValueResolver_valueFromPoin handle_errors(&mut env, result) }, "valueFromPointer", - JObject::null(), ) + .unwrap_or(JObject::null()) } #[no_mangle] @@ -135,8 +135,8 @@ pub extern "system" fn Java_glide_ffi_resolvers_RedisValueResolver_valueFromPoin handle_errors(&mut env, result) }, "valueFromPointerBinary", - JObject::null(), ) + .unwrap_or(JObject::null()) } #[no_mangle] @@ -172,8 +172,8 @@ pub extern "system" fn Java_glide_ffi_resolvers_SocketListenerResolver_startSock handle_errors(&mut env, result) }, "startSocketListener", - JObject::null(), ) + .unwrap_or(JObject::null()) } #[no_mangle] @@ -196,8 +196,8 @@ pub extern "system" fn Java_glide_ffi_resolvers_ScriptResolver_storeScript<'loca handle_errors(&mut env, result) }, "storeScript", - JObject::null(), ) + .unwrap_or(JObject::null()) } #[no_mangle] @@ -217,6 +217,6 @@ pub extern "system" fn Java_glide_ffi_resolvers_ScriptResolver_dropScript<'local handle_errors(&mut env, result) }, "dropScript", - (), ) + .unwrap_or(()) }