Skip to content

Commit

Permalink
Make handle_panics return Option<T> instead
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanl-bq committed Jun 27, 2024
1 parent d4298cd commit ee09467
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 13 deletions.
11 changes: 4 additions & 7 deletions java/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,28 +62,25 @@ pub fn handle_errors<T>(env: &mut JNIEnv, result: Result<T, FFIError>) -> 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
}
}
}

// This function handles Rust panics by converting them into Java exceptions and throwing them.
// `func` returns an `Option<T>` 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<T, F: std::panic::UnwindSafe + FnOnce() -> Option<T>>(
func: F,
ffi_func_name: &str,
default_value: T,
) -> T {
) -> Option<T> {
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
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion java/src/ffi_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ pub extern "system" fn Java_glide_ffi_FfiTest_handlePanics<'local>(
}
},
"handlePanics",
default_value,
)
.unwrap_or(default_value)
}

#[no_mangle]
Expand Down
10 changes: 5 additions & 5 deletions java/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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]
Expand Down Expand Up @@ -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]
Expand All @@ -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]
Expand All @@ -217,6 +217,6 @@ pub extern "system" fn Java_glide_ffi_resolvers_ScriptResolver_dropScript<'local
handle_errors(&mut env, result)
},
"dropScript",
(),
)
.unwrap_or(())
}

0 comments on commit ee09467

Please sign in to comment.