mirror of
https://github.com/tursodatabase/limbo.git
synced 2025-08-03 17:48:17 +00:00
Extract set_err_msg_and_throw_exception
to utils.rs
This commit is contained in:
parent
7028d963ba
commit
5fc5f650cd
3 changed files with 42 additions and 84 deletions
|
@ -1,5 +1,5 @@
|
|||
use crate::errors::LimboError;
|
||||
use jni::objects::JByteArray;
|
||||
use jni::objects::{JByteArray, JObject};
|
||||
use jni::JNIEnv;
|
||||
|
||||
pub(crate) fn utf8_byte_arr_to_str(
|
||||
|
@ -14,3 +14,42 @@ pub(crate) fn utf8_byte_arr_to_str(
|
|||
})?;
|
||||
Ok(str)
|
||||
}
|
||||
|
||||
/// Sets the error message and throws a Java exception.
|
||||
///
|
||||
/// This function converts the provided error message to a byte array and calls the
|
||||
/// `throwLimboException` method on the provided Java object to throw an exception.
|
||||
///
|
||||
/// # Parameters
|
||||
/// - `env`: The JNI environment.
|
||||
/// - `obj`: The Java object on which the exception will be thrown.
|
||||
/// - `err_code`: The error code corresponding to the exception. Refer to `org.github.tursodatabase.core.Codes` for the list of error codes.
|
||||
/// - `err_msg`: The error message to be included in the exception.
|
||||
///
|
||||
/// # Example
|
||||
/// ```rust
|
||||
/// set_err_msg_and_throw_exception(env, obj, Codes::SQLITE_ERROR, "An error occurred".to_string());
|
||||
/// ```
|
||||
pub fn set_err_msg_and_throw_exception<'local>(
|
||||
env: &mut JNIEnv<'local>,
|
||||
obj: JObject<'local>,
|
||||
err_code: i32,
|
||||
err_msg: String,
|
||||
) {
|
||||
let error_message_bytes = env
|
||||
.byte_array_from_slice(err_msg.as_bytes())
|
||||
.expect("Failed to convert to byte array");
|
||||
match env.call_method(
|
||||
obj,
|
||||
"throwLimboException",
|
||||
"(I[B)V",
|
||||
&[err_code.into(), (&error_message_bytes).into()],
|
||||
) {
|
||||
Ok(_) => {
|
||||
// do nothing because above method will always return Err
|
||||
}
|
||||
Err(_e) => {
|
||||
// do nothing because our java app will handle Err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue