From 7d263e20b2514e5a6253f847679c2079ed2dda1f Mon Sep 17 00:00:00 2001 From: Darius Maitia Date: Wed, 18 Sep 2024 22:22:56 -0300 Subject: [PATCH 1/7] ZError: adding ZError exception, from which SessionException, JNIException and KeyExprException inherit. --- .../src/commonMain/kotlin/io/zenoh/Logger.kt | 4 ++- .../io/zenoh/exceptions/JNIException.kt | 2 +- .../io/zenoh/exceptions/KeyExprException.kt | 4 +-- .../io/zenoh/exceptions/SessionException.kt | 4 +-- .../kotlin/io/zenoh/exceptions/ZError.kt | 20 +++++++++++++++ .../kotlin/io/zenoh/jni/JNIKeyExpr.kt | 15 +++++------ .../kotlin/io/zenoh/jni/JNIPublisher.kt | 5 ++-- .../kotlin/io/zenoh/jni/JNIQuery.kt | 7 +++--- .../kotlin/io/zenoh/jni/JNIScout.kt | 5 ++-- .../kotlin/io/zenoh/jni/JNISession.kt | 25 ++++++++++--------- .../kotlin/io/zenoh/protocol/ZBytes.kt | 5 ++-- 11 files changed, 62 insertions(+), 34 deletions(-) create mode 100644 zenoh-kotlin/src/commonMain/kotlin/io/zenoh/exceptions/ZError.kt diff --git a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/Logger.kt b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/Logger.kt index b53025772..540e004cd 100644 --- a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/Logger.kt +++ b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/Logger.kt @@ -14,6 +14,8 @@ package io.zenoh +import io.zenoh.exceptions.ZError + /** Logger class to redirect the Rust logs from Zenoh to the kotlin environment. */ internal class Logger { @@ -31,7 +33,7 @@ internal class Logger { * * See https://docs.rs/env_logger/latest/env_logger/index.html for accepted filter format. */ - @Throws(Exception::class) + @Throws(ZError::class) private external fun startLogsViaJNI(filter: String) } } diff --git a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/exceptions/JNIException.kt b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/exceptions/JNIException.kt index 1c31ef738..e29e117c5 100644 --- a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/exceptions/JNIException.kt +++ b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/exceptions/JNIException.kt @@ -20,4 +20,4 @@ package io.zenoh.exceptions * This type of exception is thrown from the native code when something goes wrong regarding the * communication between the Java/Kotlin layer and the native layer through the JNI. */ -class JNIException : Exception() +class JNIException(msg: String?) : ZError(msg) diff --git a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/exceptions/KeyExprException.kt b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/exceptions/KeyExprException.kt index f52307529..9ea4226d2 100644 --- a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/exceptions/KeyExprException.kt +++ b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/exceptions/KeyExprException.kt @@ -17,8 +17,8 @@ package io.zenoh.exceptions /** * Key expression exception. * - * This kind of exceptions are thrown from the native code when something goes wrong regarding a key expression, + * This kind of exception is thrown from the native code when something goes wrong regarding a key expression, * for instance when attempting to create a [io.zenoh.keyexpr.KeyExpr] from a string that does not respect the * key expression conventions. */ -class KeyExprException(val msg: String) : Exception() +class KeyExprException(msg: String?) : ZError(msg) diff --git a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/exceptions/SessionException.kt b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/exceptions/SessionException.kt index 3ea337b1c..9713c4c56 100644 --- a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/exceptions/SessionException.kt +++ b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/exceptions/SessionException.kt @@ -17,6 +17,6 @@ package io.zenoh.exceptions /** * Session exception. * - * This kind of exceptions are thrown from the native code when something goes wrong with a Zenoh session. + * This kind of exception is thrown from the native code when something goes wrong with a Zenoh [io.zenoh.Session]. */ -class SessionException(message: String?) : Exception(message) +class SessionException(message: String?) : ZError(message) diff --git a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/exceptions/ZError.kt b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/exceptions/ZError.kt new file mode 100644 index 000000000..7c025a8a1 --- /dev/null +++ b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/exceptions/ZError.kt @@ -0,0 +1,20 @@ +// +// Copyright (c) 2023 ZettaScale Technology +// +// This program and the accompanying materials are made available under the +// terms of the Eclipse Public License 2.0 which is available at +// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 +// which is available at https://www.apache.org/licenses/LICENSE-2.0. +// +// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 +// +// Contributors: +// ZettaScale Zenoh Team, +// + +package io.zenoh.exceptions + +/** + * A Zenoh Error. + */ +open class ZError(override val message: String? = null): Exception() \ No newline at end of file diff --git a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/jni/JNIKeyExpr.kt b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/jni/JNIKeyExpr.kt index ea0f54760..7022e5210 100644 --- a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/jni/JNIKeyExpr.kt +++ b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/jni/JNIKeyExpr.kt @@ -15,6 +15,7 @@ package io.zenoh.jni import io.zenoh.ZenohLoad +import io.zenoh.exceptions.ZError import io.zenoh.keyexpr.KeyExpr import io.zenoh.keyexpr.SetIntersectionLevel @@ -65,25 +66,25 @@ internal class JNIKeyExpr(internal val ptr: Long) { KeyExpr(concatViaJNI(keyExpr.jniKeyExpr?.ptr ?: 0, keyExpr.keyExpr, other)) } - @Throws(Exception::class) + @Throws(ZError::class) private external fun tryFromViaJNI(keyExpr: String): String - @Throws(Exception::class) + @Throws(ZError::class) private external fun autocanonizeViaJNI(keyExpr: String): String - @Throws(Exception::class) + @Throws(ZError::class) private external fun intersectsViaJNI(ptrA: Long, keyExprA: String, ptrB: Long, keyExprB: String): Boolean - @Throws(Exception::class) + @Throws(ZError::class) private external fun includesViaJNI(ptrA: Long, keyExprA: String, ptrB: Long, keyExprB: String): Boolean - @Throws(Exception::class) + @Throws(ZError::class) private external fun relationToViaJNI(ptrA: Long, keyExprA: String, ptrB: Long, keyExprB: String): Int - @Throws(Exception::class) + @Throws(ZError::class) private external fun joinViaJNI(ptrA: Long, keyExprA: String, other: String): String - @Throws(Exception::class) + @Throws(ZError::class) private external fun concatViaJNI(ptrA: Long, keyExprA: String, other: String): String } diff --git a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/jni/JNIPublisher.kt b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/jni/JNIPublisher.kt index d2b1909e4..b36f3633d 100644 --- a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/jni/JNIPublisher.kt +++ b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/jni/JNIPublisher.kt @@ -14,6 +14,7 @@ package io.zenoh.jni +import io.zenoh.exceptions.ZError import io.zenoh.prelude.Encoding import io.zenoh.protocol.IntoZBytes @@ -54,12 +55,12 @@ internal class JNIPublisher(private val ptr: Long) { freePtrViaJNI(ptr) } - @Throws(Exception::class) + @Throws(ZError::class) private external fun putViaJNI( valuePayload: ByteArray, encodingId: Int, encodingSchema: String?, attachment: ByteArray?, ptr: Long ) - @Throws(Exception::class) + @Throws(ZError::class) private external fun deleteViaJNI(attachment: ByteArray?, ptr: Long) private external fun freePtrViaJNI(ptr: Long) diff --git a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/jni/JNIQuery.kt b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/jni/JNIQuery.kt index 4d6f4b80b..1ab2de5ad 100644 --- a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/jni/JNIQuery.kt +++ b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/jni/JNIQuery.kt @@ -14,6 +14,7 @@ package io.zenoh.jni +import io.zenoh.exceptions.ZError import io.zenoh.keyexpr.KeyExpr import io.zenoh.prelude.Encoding import io.zenoh.prelude.QoS @@ -72,7 +73,7 @@ internal class JNIQuery(private val ptr: Long) { freePtrViaJNI(ptr) } - @Throws(Exception::class) + @Throws(ZError::class) private external fun replySuccessViaJNI( queryPtr: Long, keyExprPtr: Long, @@ -88,7 +89,7 @@ internal class JNIQuery(private val ptr: Long) { qosCongestionControl: Int, ) - @Throws(Exception::class) + @Throws(ZError::class) private external fun replyErrorViaJNI( queryPtr: Long, errorValuePayload: ByteArray, @@ -96,7 +97,7 @@ internal class JNIQuery(private val ptr: Long) { encodingSchema: String?, ) - @Throws(Exception::class) + @Throws(ZError::class) private external fun replyDeleteViaJNI( queryPtr: Long, keyExprPtr: Long, diff --git a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/jni/JNIScout.kt b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/jni/JNIScout.kt index c89684507..980991d55 100644 --- a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/jni/JNIScout.kt +++ b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/jni/JNIScout.kt @@ -15,6 +15,7 @@ package io.zenoh.jni import io.zenoh.Config +import io.zenoh.exceptions.ZError import io.zenoh.handlers.Callback import io.zenoh.jni.callbacks.JNIScoutCallback import io.zenoh.protocol.ZenohID @@ -44,14 +45,14 @@ internal class JNIScout(private val ptr: Long) { Scout(receiver, JNIScout(ptr)) } - @Throws(Exception::class) + @Throws(ZError::class) private external fun scoutViaJNI( whatAmI: Int, callback: JNIScoutCallback, configPtr: Long, ): Long - @Throws(Exception::class) + @Throws(ZError::class) external fun freePtrViaJNI(ptr: Long) } diff --git a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/jni/JNISession.kt b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/jni/JNISession.kt index f212b013b..3c2bfb5bd 100644 --- a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/jni/JNISession.kt +++ b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/jni/JNISession.kt @@ -17,6 +17,7 @@ package io.zenoh.jni import io.zenoh.* import io.zenoh.prelude.Encoding import io.zenoh.exceptions.SessionException +import io.zenoh.exceptions.ZError import io.zenoh.handlers.Callback import io.zenoh.jni.callbacks.JNIOnCloseCallback import io.zenoh.jni.callbacks.JNIGetCallback @@ -214,7 +215,7 @@ internal class JNISession { } ?: throw SessionException("Attempting to undeclare a non declared key expression.") } - @Throws(Exception::class) + @Throws(ZError::class) fun performPut( keyExpr: KeyExpr, put: Put, @@ -234,7 +235,7 @@ internal class JNISession { ) } - @Throws(Exception::class) + @Throws(ZError::class) fun performDelete( keyExpr: KeyExpr, delete: Delete, @@ -272,13 +273,13 @@ internal class JNISession { @Throws(Exception::class) private external fun getRoutersZidViaJNI(ptr: Long): List - @Throws(Exception::class) + @Throws(ZError::class) private external fun openSessionViaJNI(configPtr: Long): Long - @Throws(Exception::class) + @Throws(ZError::class) private external fun closeSessionViaJNI(ptr: Long) - @Throws(Exception::class) + @Throws(ZError::class) private external fun declarePublisherViaJNI( keyExprPtr: Long, keyExprString: String, @@ -289,7 +290,7 @@ internal class JNISession { reliability: Int ): Long - @Throws(Exception::class) + @Throws(ZError::class) private external fun declareSubscriberViaJNI( keyExprPtr: Long, keyExprString: String, @@ -298,7 +299,7 @@ internal class JNISession { onClose: JNIOnCloseCallback, ): Long - @Throws(Exception::class) + @Throws(ZError::class) private external fun declareQueryableViaJNI( keyExprPtr: Long, keyExprString: String, @@ -308,13 +309,13 @@ internal class JNISession { complete: Boolean ): Long - @Throws(Exception::class) + @Throws(ZError::class) private external fun declareKeyExprViaJNI(sessionPtr: Long, keyExpr: String): Long - @Throws(Exception::class) + @Throws(ZError::class) private external fun undeclareKeyExprViaJNI(sessionPtr: Long, keyExprPtr: Long) - @Throws(Exception::class) + @Throws(ZError::class) private external fun getViaJNI( keyExprPtr: Long, keyExprString: String, @@ -331,7 +332,7 @@ internal class JNISession { encodingSchema: String?, ) - @Throws(Exception::class) + @Throws(ZError::class) private external fun putViaJNI( keyExprPtr: Long, keyExprString: String, @@ -346,7 +347,7 @@ internal class JNISession { reliability: Int ) - @Throws(Exception::class) + @Throws(ZError::class) private external fun deleteViaJNI( keyExprPtr: Long, keyExprString: String, diff --git a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/protocol/ZBytes.kt b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/protocol/ZBytes.kt index cfe0229ea..e3ff25e73 100644 --- a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/protocol/ZBytes.kt +++ b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/protocol/ZBytes.kt @@ -14,6 +14,7 @@ package io.zenoh.protocol +import io.zenoh.exceptions.ZError import io.zenoh.jni.JNIZBytes import java.nio.ByteBuffer import java.nio.ByteOrder @@ -481,7 +482,7 @@ fun ByteArray.into(): ZBytes { return ZBytes(this) } -@Throws(Exception::class) +@Throws(ZError::class) internal fun Any?.into(): ZBytes { return when (this) { is String -> this.into() @@ -492,7 +493,7 @@ internal fun Any?.into(): ZBytes { } } -@Throws(Exception::class) +@Throws(ZError::class) internal fun ZBytes.intoAny(type: KType): Any { return when (type) { typeOf() -> this.toString() From 000dbd926c7b102f7781908f36444891ff7e8ff0 Mon Sep 17 00:00:00 2001 From: Darius Maitia Date: Wed, 18 Sep 2024 22:48:20 -0300 Subject: [PATCH 2/7] feat(ZError): correcting error types on JNI code --- zenoh-jni/src/config.rs | 16 ++++++++-------- zenoh-jni/src/errors.rs | 13 +++++++++++++ zenoh-jni/src/key_expr.rs | 6 +++--- zenoh-jni/src/zbytes.rs | 4 ++-- zenoh-jni/src/zenoh_id.rs | 4 ++-- .../kotlin/io/zenoh/exceptions/ZError.kt | 2 +- 6 files changed, 29 insertions(+), 16 deletions(-) diff --git a/zenoh-jni/src/config.rs b/zenoh-jni/src/config.rs index 6bd49d905..5bd4c1b01 100644 --- a/zenoh-jni/src/config.rs +++ b/zenoh-jni/src/config.rs @@ -21,7 +21,7 @@ use jni::{ }; use zenoh::Config; -use crate::{errors::Result, jni_error}; +use crate::{errors::Result, jni_error, zerror}; use crate::{session_error, throw_exception, utils::decode_string}; /// Loads the default configuration, returning a raw pointer to it. @@ -79,10 +79,10 @@ pub extern "C" fn Java_io_zenoh_jni_JNIConfig_00024Companion_loadJsonConfigViaJN || -> Result<*const Config> { let json_config = decode_string(&mut env, &json_config)?; let mut deserializer = - json5::Deserializer::from_str(&json_config).map_err(|err| session_error!(err))?; + json5::Deserializer::from_str(&json_config).map_err(|err| zerror!(err))?; let config = Config::from_deserializer(&mut deserializer).map_err(|err| match err { - Ok(c) => session_error!("Invalid configuration: {}", c), - Err(e) => session_error!("JSON error: {}", e), + Ok(c) => zerror!("Invalid configuration: {}", c), + Err(e) => zerror!("JSON error: {}", e), })?; Ok(Arc::into_raw(Arc::new(config))) }() @@ -109,8 +109,8 @@ pub extern "C" fn Java_io_zenoh_jni_JNIConfig_00024Companion_loadYamlConfigViaJN let yaml_config = decode_string(&mut env, &yaml_config)?; let deserializer = serde_yaml::Deserializer::from_str(&yaml_config); let config = Config::from_deserializer(deserializer).map_err(|err| match err { - Ok(c) => session_error!("Invalid configuration: {}", c), - Err(e) => session_error!("YAML error: {}", e), + Ok(c) => zerror!("Invalid configuration: {}", c), + Err(e) => zerror!("YAML error: {}", e), })?; Ok(Arc::into_raw(Arc::new(config))) }() @@ -133,7 +133,7 @@ pub unsafe extern "C" fn Java_io_zenoh_jni_JNIConfig_00024Companion_getJsonViaJN let arc_cfg: Arc = Arc::from_raw(cfg_ptr); let result = || -> Result { let key = decode_string(&mut env, &key)?; - let json = arc_cfg.get_json(&key).map_err(|err| session_error!(err))?; + let json = arc_cfg.get_json(&key).map_err(|err| zerror!(err))?; let java_json = env.new_string(json).map_err(|err| jni_error!(err))?; Ok(java_json.as_raw()) }() @@ -162,7 +162,7 @@ pub unsafe extern "C" fn Java_io_zenoh_jni_JNIConfig_00024Companion_insertJson5V let mut config = core::ptr::read(cfg_ptr); let insert_result = config .insert_json5(&key, &value) - .map_err(|err| session_error!(err)); + .map_err(|err| zerror!(err)); core::ptr::write(cfg_ptr as *mut _, config); insert_result }() diff --git a/zenoh-jni/src/errors.rs b/zenoh-jni/src/errors.rs index 5afe57730..139d1e6ea 100644 --- a/zenoh-jni/src/errors.rs +++ b/zenoh-jni/src/errors.rs @@ -25,6 +25,16 @@ macro_rules! throw_exception { }}; } +#[macro_export] +macro_rules! zerror { + ($arg:expr) => { + $crate::errors::Error::ZenohErr($arg.to_string()) + }; + ($fmt:expr, $($arg:tt)*) => { + $crate::errors::Error::ZenohErr(format!($fmt, $($arg)*)) + }; +} + #[macro_export] macro_rules! jni_error { ($arg:expr) => { @@ -63,6 +73,7 @@ pub(crate) enum Error { Session(String), KeyExpr(String), Jni(String), + ZenohErr(String), } impl fmt::Display for Error { @@ -71,6 +82,7 @@ impl fmt::Display for Error { Error::Session(msg) => write!(f, "{}", msg), Error::KeyExpr(msg) => write!(f, "{}", msg), Error::Jni(msg) => write!(f, "{}", msg), + Error::ZenohErr(msg) => write!(f, "{}", msg), } } } @@ -81,6 +93,7 @@ impl Error { Error::Session(_) => "io/zenoh/exceptions/SessionException", Error::KeyExpr(_) => "io/zenoh/exceptions/KeyExprException", Error::Jni(_) => "io/zenoh/exceptions/JNIException", + Error::ZenohErr(_) => "io/zenoh/exceptions/ZError", }; class.to_string() } diff --git a/zenoh-jni/src/key_expr.rs b/zenoh-jni/src/key_expr.rs index a329e618b..27f406aaf 100644 --- a/zenoh-jni/src/key_expr.rs +++ b/zenoh-jni/src/key_expr.rs @@ -23,7 +23,7 @@ use zenoh::key_expr::KeyExpr; use crate::errors::Error; use crate::errors::Result; use crate::utils::decode_string; -use crate::{jni_error, key_expr_error, session_error, throw_exception}; +use crate::{jni_error, key_expr_error, throw_exception}; /// Validates the provided `key_expr` to be a valid key expression, returning it back /// in case of success or throwing an exception in case of failure. @@ -209,7 +209,7 @@ pub unsafe extern "C" fn Java_io_zenoh_jni_JNIKeyExpr_00024Companion_joinViaJNI( let key_expr_2_str = decode_string(&mut env, &key_expr_2)?; let result = key_expr_1 .join(key_expr_2_str.as_str()) - .map_err(|err| session_error!(err))?; + .map_err(|err| key_expr_error!(err))?; env.new_string(result.to_string()) .map(|kexp| kexp.as_raw()) .map_err(|err| jni_error!(err)) @@ -248,7 +248,7 @@ pub unsafe extern "C" fn Java_io_zenoh_jni_JNIKeyExpr_00024Companion_concatViaJN let key_expr_2_str = decode_string(&mut env, &key_expr_2)?; let result = key_expr_1 .concat(key_expr_2_str.as_str()) - .map_err(|err| session_error!(err))?; + .map_err(|err| key_expr_error!(err))?; env.new_string(result.to_string()) .map(|kexp| kexp.as_raw()) .map_err(|err| jni_error!(err)) diff --git a/zenoh-jni/src/zbytes.rs b/zenoh-jni/src/zbytes.rs index da067f499..fff9a25df 100644 --- a/zenoh-jni/src/zbytes.rs +++ b/zenoh-jni/src/zbytes.rs @@ -21,7 +21,7 @@ use jni::{ }; use zenoh::bytes::ZBytes; -use crate::{errors::Result, jni_error, session_error, utils::bytes_to_java_array}; +use crate::{errors::Result, jni_error, utils::bytes_to_java_array, zerror}; use crate::{throw_exception, utils::decode_byte_array}; /// @@ -89,7 +89,7 @@ pub extern "C" fn Java_io_zenoh_jni_JNIZBytes_deserializeIntoMapViaJNI( let zbytes = ZBytes::new(payload); let deserialization: HashMap, Vec> = zbytes .deserialize::, Vec>>() - .map_err(|err| session_error!(err))?; + .map_err(|err| zerror!(err))?; hashmap_to_java_map(&mut env, &deserialization).map_err(|err| jni_error!(err)) }() .unwrap_or_else(|err| { diff --git a/zenoh-jni/src/zenoh_id.rs b/zenoh-jni/src/zenoh_id.rs index fc0dbf1c2..192131ae5 100644 --- a/zenoh-jni/src/zenoh_id.rs +++ b/zenoh-jni/src/zenoh_id.rs @@ -12,7 +12,7 @@ // ZettaScale Zenoh Team, // -use crate::{errors::Result, jni_error, session_error, throw_exception, utils::decode_byte_array}; +use crate::{errors::Result, jni_error, throw_exception, utils::decode_byte_array, zerror}; use jni::{ objects::{JByteArray, JClass, JString}, sys::jstring, @@ -30,7 +30,7 @@ pub extern "C" fn Java_io_zenoh_jni_JNIZenohID_toStringViaJNI( ) -> jstring { || -> Result { let bytes = decode_byte_array(&env, zenoh_id)?; - let zenohid = ZenohId::try_from(bytes.as_slice()).map_err(|err| session_error!(err))?; + let zenohid = ZenohId::try_from(bytes.as_slice()).map_err(|err| zerror!(err))?; env.new_string(zenohid.to_string()) .map_err(|err| jni_error!(err)) }() diff --git a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/exceptions/ZError.kt b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/exceptions/ZError.kt index 7c025a8a1..59519379a 100644 --- a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/exceptions/ZError.kt +++ b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/exceptions/ZError.kt @@ -17,4 +17,4 @@ package io.zenoh.exceptions /** * A Zenoh Error. */ -open class ZError(override val message: String? = null): Exception() \ No newline at end of file +open class ZError(override val message: String? = null): Exception() From 75e6cd9bc926f3a87a589186a734c602fc2630aa Mon Sep 17 00:00:00 2001 From: Darius Maitia Date: Wed, 18 Sep 2024 22:53:10 -0300 Subject: [PATCH 3/7] ZError: specifying error type on JNIConfig. --- .../commonMain/kotlin/io/zenoh/jni/JNIConfig.kt | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/jni/JNIConfig.kt b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/jni/JNIConfig.kt index 7ca8e5c63..4c7c0344f 100644 --- a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/jni/JNIConfig.kt +++ b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/jni/JNIConfig.kt @@ -16,6 +16,7 @@ package io.zenoh.jni import io.zenoh.Config import io.zenoh.ZenohLoad +import io.zenoh.exceptions.ZError import java.io.File import java.nio.file.Path @@ -54,28 +55,28 @@ internal class JNIConfig(internal val ptr: Long) { Config(JNIConfig(cfgPtr)) } - @Throws + @Throws(ZError::class) private external fun loadDefaultConfigViaJNI(): Long - @Throws + @Throws(ZError::class) private external fun loadConfigFileViaJNI(path: String): Long - @Throws + @Throws(ZError::class) private external fun loadJsonConfigViaJNI(rawConfig: String): Long - @Throws + @Throws(ZError::class) private external fun loadYamlConfigViaJNI(rawConfig: String): Long - @Throws + @Throws(ZError::class) private external fun getIdViaJNI(ptr: Long): ByteArray - @Throws + @Throws(ZError::class) private external fun insertJson5ViaJNI(ptr: Long, key: String, value: String): Long /** Frees the underlying native config. */ private external fun freePtrViaJNI(ptr: Long) - @Throws + @Throws(ZError::class) private external fun getJsonViaJNI(ptr: Long, key: String): String } From 6d5f8bfc9a96be6feb81ba0d7aee1b8b814c1777 Mon Sep 17 00:00:00 2001 From: Darius Maitia Date: Thu, 19 Sep 2024 05:07:12 -0300 Subject: [PATCH 4/7] ZError: fix tests --- zenoh-kotlin/src/commonTest/kotlin/io/zenoh/ConfigTest.kt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/zenoh-kotlin/src/commonTest/kotlin/io/zenoh/ConfigTest.kt b/zenoh-kotlin/src/commonTest/kotlin/io/zenoh/ConfigTest.kt index 241f88353..7da196998 100644 --- a/zenoh-kotlin/src/commonTest/kotlin/io/zenoh/ConfigTest.kt +++ b/zenoh-kotlin/src/commonTest/kotlin/io/zenoh/ConfigTest.kt @@ -14,6 +14,7 @@ package io.zenoh import io.zenoh.exceptions.SessionException +import io.zenoh.exceptions.ZError import io.zenoh.keyexpr.intoKeyExpr import io.zenoh.protocol.into import io.zenoh.sample.Sample @@ -197,7 +198,7 @@ class ConfigTest { """.trimIndent() val config = Config.fromJson(illFormatedConfig) assertTrue(config.isFailure) - assertThrows { config.getOrThrow() } + assertThrows { config.getOrThrow() } } @Test @@ -211,7 +212,7 @@ class ConfigTest { """.trimIndent() val config = Config.fromJson(illFormatedConfig) assertTrue(config.isFailure) - assertThrows { config.getOrThrow() } + assertThrows { config.getOrThrow() } } @Test From 1705280184f4ea1e6b626194d6bf9a833b0de7fd Mon Sep 17 00:00:00 2001 From: Darius Maitia Date: Thu, 19 Sep 2024 10:57:19 -0300 Subject: [PATCH 5/7] ZError: removing JNIException.kt, SessionException.kt and KeyExprException.kt. Using ZError instead. --- zenoh-jni/src/config.rs | 18 +-- zenoh-jni/src/errors.rs | 74 ++---------- zenoh-jni/src/key_expr.rs | 41 ++++--- zenoh-jni/src/logger.rs | 10 +- zenoh-jni/src/publisher.rs | 14 +-- zenoh-jni/src/query.rs | 16 +-- zenoh-jni/src/scouting.rs | 11 +- zenoh-jni/src/session.rs | 112 +++++++++--------- zenoh-jni/src/utils.rs | 61 +++++----- zenoh-jni/src/zbytes.rs | 18 +-- zenoh-jni/src/zenoh_id.rs | 6 +- .../src/commonMain/kotlin/io/zenoh/Session.kt | 4 +- .../io/zenoh/exceptions/JNIException.kt | 23 ---- .../io/zenoh/exceptions/KeyExprException.kt | 24 ---- .../io/zenoh/exceptions/SessionException.kt | 22 ---- .../kotlin/io/zenoh/exceptions/ZError.kt | 2 +- .../kotlin/io/zenoh/jni/JNISession.kt | 3 +- .../kotlin/io/zenoh/keyexpr/IntoKeyExpr.kt | 5 +- .../kotlin/io/zenoh/publication/Publisher.kt | 4 +- .../kotlin/io/zenoh/queryable/Query.kt | 8 +- .../kotlin/io/zenoh/selector/IntoSelector.kt | 5 +- .../commonTest/kotlin/io/zenoh/ConfigTest.kt | 1 - .../commonTest/kotlin/io/zenoh/KeyExprTest.kt | 3 - .../kotlin/io/zenoh/SelectorTest.kt | 4 +- .../commonTest/kotlin/io/zenoh/SessionTest.kt | 9 +- 25 files changed, 180 insertions(+), 318 deletions(-) delete mode 100644 zenoh-kotlin/src/commonMain/kotlin/io/zenoh/exceptions/JNIException.kt delete mode 100644 zenoh-kotlin/src/commonMain/kotlin/io/zenoh/exceptions/KeyExprException.kt delete mode 100644 zenoh-kotlin/src/commonMain/kotlin/io/zenoh/exceptions/SessionException.kt diff --git a/zenoh-jni/src/config.rs b/zenoh-jni/src/config.rs index 5bd4c1b01..0ada13407 100644 --- a/zenoh-jni/src/config.rs +++ b/zenoh-jni/src/config.rs @@ -21,8 +21,8 @@ use jni::{ }; use zenoh::Config; -use crate::{errors::Result, jni_error, zerror}; -use crate::{session_error, throw_exception, utils::decode_string}; +use crate::{errors::ZResult, zerror}; +use crate::{throw_exception, utils::decode_string}; /// Loads the default configuration, returning a raw pointer to it. /// @@ -52,9 +52,9 @@ pub extern "C" fn Java_io_zenoh_jni_JNIConfig_00024Companion_loadConfigFileViaJN _class: JClass, config_path: JString, ) -> *const Config { - || -> Result<*const Config> { + || -> ZResult<*const Config> { let config_file_path = decode_string(&mut env, &config_path)?; - let config = Config::from_file(config_file_path).map_err(|err| session_error!(err))?; + let config = Config::from_file(config_file_path).map_err(|err| zerror!(err))?; Ok(Arc::into_raw(Arc::new(config))) }() .unwrap_or_else(|err| { @@ -76,7 +76,7 @@ pub extern "C" fn Java_io_zenoh_jni_JNIConfig_00024Companion_loadJsonConfigViaJN _class: JClass, json_config: JString, ) -> *const Config { - || -> Result<*const Config> { + || -> ZResult<*const Config> { let json_config = decode_string(&mut env, &json_config)?; let mut deserializer = json5::Deserializer::from_str(&json_config).map_err(|err| zerror!(err))?; @@ -105,7 +105,7 @@ pub extern "C" fn Java_io_zenoh_jni_JNIConfig_00024Companion_loadYamlConfigViaJN _class: JClass, yaml_config: JString, ) -> *const Config { - || -> Result<*const Config> { + || -> ZResult<*const Config> { let yaml_config = decode_string(&mut env, &yaml_config)?; let deserializer = serde_yaml::Deserializer::from_str(&yaml_config); let config = Config::from_deserializer(deserializer).map_err(|err| match err { @@ -131,10 +131,10 @@ pub unsafe extern "C" fn Java_io_zenoh_jni_JNIConfig_00024Companion_getJsonViaJN key: JString, ) -> jstring { let arc_cfg: Arc = Arc::from_raw(cfg_ptr); - let result = || -> Result { + let result = || -> ZResult { let key = decode_string(&mut env, &key)?; let json = arc_cfg.get_json(&key).map_err(|err| zerror!(err))?; - let java_json = env.new_string(json).map_err(|err| jni_error!(err))?; + let java_json = env.new_string(json).map_err(|err| zerror!(err))?; Ok(java_json.as_raw()) }() .unwrap_or_else(|err| { @@ -156,7 +156,7 @@ pub unsafe extern "C" fn Java_io_zenoh_jni_JNIConfig_00024Companion_insertJson5V key: JString, value: JString, ) { - || -> Result<()> { + || -> ZResult<()> { let key = decode_string(&mut env, &key)?; let value = decode_string(&mut env, &value)?; let mut config = core::ptr::read(cfg_ptr); diff --git a/zenoh-jni/src/errors.rs b/zenoh-jni/src/errors.rs index 139d1e6ea..23687d4cc 100644 --- a/zenoh-jni/src/errors.rs +++ b/zenoh-jni/src/errors.rs @@ -28,82 +28,32 @@ macro_rules! throw_exception { #[macro_export] macro_rules! zerror { ($arg:expr) => { - $crate::errors::Error::ZenohErr($arg.to_string()) + $crate::errors::ZError($arg.to_string()) }; ($fmt:expr, $($arg:tt)*) => { - $crate::errors::Error::ZenohErr(format!($fmt, $($arg)*)) + $crate::errors::ZError(format!($fmt, $($arg)*)) }; } -#[macro_export] -macro_rules! jni_error { - ($arg:expr) => { - $crate::errors::Error::Jni($arg.to_string()) - }; - ($fmt:expr, $($arg:tt)*) => { - $crate::errors::Error::Jni(format!($fmt, $($arg)*)) - }; -} - -#[macro_export] -macro_rules! session_error { - ($arg:expr) => { - $crate::errors::Error::Session($arg.to_string()) - }; - ($fmt:expr, $($arg:tt)*) => { - $crate::errors::Error::Session(format!($fmt, $($arg)*)) - }; - -} - -#[macro_export] -macro_rules! key_expr_error { - ($arg:expr) => { - Error::KeyExpr($arg.to_string()) - }; - ($fmt:expr, $($arg:tt)*) => { - Error::KeyExpr(format!($fmt, $($arg)*)) - }; -} - -pub(crate) type Result = core::result::Result; +pub(crate) type ZResult = core::result::Result; #[derive(Debug)] -pub(crate) enum Error { - Session(String), - KeyExpr(String), - Jni(String), - ZenohErr(String), -} +pub(crate) struct ZError(pub String); -impl fmt::Display for Error { +impl fmt::Display for ZError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Error::Session(msg) => write!(f, "{}", msg), - Error::KeyExpr(msg) => write!(f, "{}", msg), - Error::Jni(msg) => write!(f, "{}", msg), - Error::ZenohErr(msg) => write!(f, "{}", msg), - } + write!(f, "{}", self.0) } } -impl Error { - fn get_associated_kotlin_exception(&self) -> String { - let class = match self { - Error::Session(_) => "io/zenoh/exceptions/SessionException", - Error::KeyExpr(_) => "io/zenoh/exceptions/KeyExprException", - Error::Jni(_) => "io/zenoh/exceptions/JNIException", - Error::ZenohErr(_) => "io/zenoh/exceptions/ZError", - }; - class.to_string() - } +impl ZError { + const KOTLIN_EXCEPTION_NAME: &'static str = "io/zenoh/exceptions/ZError"; - pub fn throw_on_jvm(&self, env: &mut JNIEnv) -> Result<()> { - let exception_name = self.get_associated_kotlin_exception(); + pub fn throw_on_jvm(&self, env: &mut JNIEnv) -> ZResult<()> { let exception_class = env - .find_class(&exception_name) - .map_err(|err| jni_error!("Failed to retrieve exception class: {}", err))?; + .find_class(Self::KOTLIN_EXCEPTION_NAME) + .map_err(|err| zerror!("Failed to retrieve exception class: {}", err))?; env.throw_new(exception_class, self.to_string()) - .map_err(|err| jni_error!("Failed to throw exception: {}", err)) + .map_err(|err| zerror!("Failed to throw exception: {}", err)) } } diff --git a/zenoh-jni/src/key_expr.rs b/zenoh-jni/src/key_expr.rs index 27f406aaf..06a1b6c64 100644 --- a/zenoh-jni/src/key_expr.rs +++ b/zenoh-jni/src/key_expr.rs @@ -20,10 +20,9 @@ use jni::sys::{jboolean, jint, jstring}; use jni::{objects::JString, JNIEnv}; use zenoh::key_expr::KeyExpr; -use crate::errors::Error; -use crate::errors::Result; +use crate::errors::ZResult; use crate::utils::decode_string; -use crate::{jni_error, key_expr_error, throw_exception}; +use crate::{throw_exception, zerror}; /// Validates the provided `key_expr` to be a valid key expression, returning it back /// in case of success or throwing an exception in case of failure. @@ -67,7 +66,7 @@ pub extern "C" fn Java_io_zenoh_jni_JNIKeyExpr_00024Companion_autocanonizeViaJNI .and_then(|key_expr| { env.new_string(key_expr.to_string()) .map(|kexp| kexp.as_raw()) - .map_err(|err| jni_error!(err)) + .map_err(|err| zerror!(err)) }) .unwrap_or_else(|err| { throw_exception!(env, err); @@ -99,7 +98,7 @@ pub unsafe extern "C" fn Java_io_zenoh_jni_JNIKeyExpr_00024Companion_intersectsV key_expr_ptr_2: /*nullable*/ *const KeyExpr<'static>, key_expr_str_2: JString, ) -> jboolean { - || -> Result { + || -> ZResult { let key_expr_1 = process_kotlin_key_expr(&mut env, &key_expr_str_1, key_expr_ptr_1)?; let key_expr_2 = process_kotlin_key_expr(&mut env, &key_expr_str_2, key_expr_ptr_2)?; Ok(key_expr_1.intersects(&key_expr_2) as jboolean) @@ -134,7 +133,7 @@ pub unsafe extern "C" fn Java_io_zenoh_jni_JNIKeyExpr_00024Companion_includesVia key_expr_ptr_2: /*nullable*/ *const KeyExpr<'static>, key_expr_str_2: JString, ) -> jboolean { - || -> Result { + || -> ZResult { let key_expr_1 = process_kotlin_key_expr(&mut env, &key_expr_str_1, key_expr_ptr_1)?; let key_expr_2 = process_kotlin_key_expr(&mut env, &key_expr_str_2, key_expr_ptr_2)?; Ok(key_expr_1.includes(&key_expr_2) as jboolean) @@ -170,7 +169,7 @@ pub unsafe extern "C" fn Java_io_zenoh_jni_JNIKeyExpr_00024Companion_relationToV key_expr_ptr_2: /*nullable*/ *const KeyExpr<'static>, key_expr_str_2: JString, ) -> jint { - || -> Result { + || -> ZResult { let key_expr_1 = process_kotlin_key_expr(&mut env, &key_expr_str_1, key_expr_ptr_1)?; let key_expr_2 = process_kotlin_key_expr(&mut env, &key_expr_str_2, key_expr_ptr_2)?; Ok(key_expr_1.relation_to(&key_expr_2) as jint) @@ -204,15 +203,15 @@ pub unsafe extern "C" fn Java_io_zenoh_jni_JNIKeyExpr_00024Companion_joinViaJNI( key_expr_str_1: JString, key_expr_2: JString, ) -> jstring { - || -> Result { + || -> ZResult { let key_expr_1 = process_kotlin_key_expr(&mut env, &key_expr_str_1, key_expr_ptr_1)?; let key_expr_2_str = decode_string(&mut env, &key_expr_2)?; let result = key_expr_1 .join(key_expr_2_str.as_str()) - .map_err(|err| key_expr_error!(err))?; + .map_err(|err| zerror!(err))?; env.new_string(result.to_string()) .map(|kexp| kexp.as_raw()) - .map_err(|err| jni_error!(err)) + .map_err(|err| zerror!(err)) }() .unwrap_or_else(|err| { throw_exception!(env, err); @@ -243,15 +242,15 @@ pub unsafe extern "C" fn Java_io_zenoh_jni_JNIKeyExpr_00024Companion_concatViaJN key_expr_str_1: JString, key_expr_2: JString, ) -> jstring { - || -> Result { + || -> ZResult { let key_expr_1 = process_kotlin_key_expr(&mut env, &key_expr_str_1, key_expr_ptr_1)?; let key_expr_2_str = decode_string(&mut env, &key_expr_2)?; let result = key_expr_1 .concat(key_expr_2_str.as_str()) - .map_err(|err| key_expr_error!(err))?; + .map_err(|err| zerror!(err))?; env.new_string(result.to_string()) .map(|kexp| kexp.as_raw()) - .map_err(|err| jni_error!(err)) + .map_err(|err| zerror!(err)) }() .unwrap_or_else(|err| { throw_exception!(env, err); @@ -281,20 +280,20 @@ pub unsafe extern "C" fn Java_io_zenoh_jni_JNIKeyExpr_freePtrViaJNI( Arc::from_raw(key_expr_ptr); } -fn validate_key_expr(env: &mut JNIEnv, key_expr: &JString) -> Result> { +fn validate_key_expr(env: &mut JNIEnv, key_expr: &JString) -> ZResult> { let key_expr_str = decode_string(env, key_expr) - .map_err(|err| jni_error!("Unable to get key expression string value: '{}'.", err))?; + .map_err(|err| zerror!("Unable to get key expression string value: '{}'.", err))?; KeyExpr::try_from(key_expr_str) - .map_err(|err| key_expr_error!("Unable to create key expression: '{}'.", err)) + .map_err(|err| zerror!("Unable to create key expression: '{}'.", err)) } -fn autocanonize_key_expr(env: &mut JNIEnv, key_expr: &JString) -> Result> { +fn autocanonize_key_expr(env: &mut JNIEnv, key_expr: &JString) -> ZResult> { decode_string(env, key_expr) - .map_err(|err| jni_error!("Unable to get key expression string value: '{}'.", err)) + .map_err(|err| zerror!("Unable to get key expression string value: '{}'.", err)) .and_then(|key_expr_str| { KeyExpr::autocanonize(key_expr_str) - .map_err(|err| key_expr_error!("Unable to create key expression: '{}'", err)) + .map_err(|err| zerror!("Unable to create key expression: '{}'", err)) }) } @@ -315,10 +314,10 @@ pub(crate) unsafe fn process_kotlin_key_expr( env: &mut JNIEnv, key_expr_str: &JString, key_expr_ptr: *const KeyExpr<'static>, -) -> Result> { +) -> ZResult> { if key_expr_ptr.is_null() { let key_expr = decode_string(env, key_expr_str) - .map_err(|err| jni_error!("Unable to get key expression string value: '{}'.", err))?; + .map_err(|err| zerror!("Unable to get key expression string value: '{}'.", err))?; Ok(KeyExpr::from_string_unchecked(key_expr)) } else { let key_expr = Arc::from_raw(key_expr_ptr); diff --git a/zenoh-jni/src/logger.rs b/zenoh-jni/src/logger.rs index 1011c905f..73f5112c9 100644 --- a/zenoh-jni/src/logger.rs +++ b/zenoh-jni/src/logger.rs @@ -17,7 +17,7 @@ use jni::{ JNIEnv, }; -use crate::{errors::Result, jni_error, throw_exception}; +use crate::{errors::ZResult, throw_exception, zerror}; /// Redirects the Rust logs either to logcat for Android systems or to the standard output (for non-Android systems). /// @@ -41,7 +41,7 @@ pub extern "C" fn Java_io_zenoh_Logger_00024Companion_startLogsViaJNI( _class: JClass, filter: JString, ) { - || -> Result<()> { + || -> ZResult<()> { let log_level = parse_filter(&mut env, filter)?; android_logd_logger::builder() .parse_filters(log_level.as_str()) @@ -53,10 +53,10 @@ pub extern "C" fn Java_io_zenoh_Logger_00024Companion_startLogsViaJNI( .unwrap_or_else(|err| throw_exception!(env, err)) } -fn parse_filter(env: &mut JNIEnv, log_level: JString) -> Result { - let log_level = env.get_string(&log_level).map_err(|err| jni_error!(err))?; +fn parse_filter(env: &mut JNIEnv, log_level: JString) -> ZResult { + let log_level = env.get_string(&log_level).map_err(|err| zerror!(err))?; log_level .to_str() .map(|level| Ok(level.to_string())) - .map_err(|err| jni_error!(err))? + .map_err(|err| zerror!(err))? } diff --git a/zenoh-jni/src/publisher.rs b/zenoh-jni/src/publisher.rs index c6607cd08..91219057a 100644 --- a/zenoh-jni/src/publisher.rs +++ b/zenoh-jni/src/publisher.rs @@ -22,10 +22,10 @@ use jni::{ use zenoh::{pubsub::Publisher, Wait}; use crate::{ - errors::Result, - utils::{decode_byte_array, decode_encoding}, + errors::ZResult, + utils::{decode_byte_array, decode_encoding}, zerror, }; -use crate::{session_error, throw_exception}; +use crate::{throw_exception}; /// Performs a PUT operation on a Zenoh publisher via JNI. /// @@ -56,7 +56,7 @@ pub unsafe extern "C" fn Java_io_zenoh_jni_JNIPublisher_putViaJNI( publisher_ptr: *const Publisher<'static>, ) { let publisher = Arc::from_raw(publisher_ptr); - let _ = || -> Result<()> { + let _ = || -> ZResult<()> { let payload = decode_byte_array(&env, payload)?; let mut publication = publisher.put(payload); let encoding = decode_encoding(&mut env, encoding_id, &encoding_schema)?; @@ -65,7 +65,7 @@ pub unsafe extern "C" fn Java_io_zenoh_jni_JNIPublisher_putViaJNI( let attachment = decode_byte_array(&env, attachment)?; publication = publication.attachment::>(attachment) }; - publication.wait().map_err(|err| session_error!(err)) + publication.wait().map_err(|err| zerror!(err)) }() .map_err(|err| throw_exception!(env, err)); std::mem::forget(publisher); @@ -94,13 +94,13 @@ pub unsafe extern "C" fn Java_io_zenoh_jni_JNIPublisher_deleteViaJNI( publisher_ptr: *const Publisher<'static>, ) { let publisher = Arc::from_raw(publisher_ptr); - let _ = || -> Result<()> { + let _ = || -> ZResult<()> { let mut delete = publisher.delete(); if !attachment.is_null() { let attachment = decode_byte_array(&env, attachment)?; delete = delete.attachment::>(attachment) }; - delete.wait().map_err(|err| session_error!(err)) + delete.wait().map_err(|err| zerror!(err)) }() .map_err(|err| throw_exception!(env, err)); std::mem::forget(publisher) diff --git a/zenoh-jni/src/query.rs b/zenoh-jni/src/query.rs index f7b557ec2..4e1da0e69 100644 --- a/zenoh-jni/src/query.rs +++ b/zenoh-jni/src/query.rs @@ -14,9 +14,9 @@ use std::sync::Arc; -use crate::{errors::Result, key_expr::process_kotlin_key_expr, throw_exception}; +use crate::zerror; +use crate::{errors::ZResult, key_expr::process_kotlin_key_expr, throw_exception}; use crate::{ - session_error, utils::{decode_byte_array, decode_encoding}, }; use jni::{ @@ -76,7 +76,7 @@ pub(crate) unsafe extern "C" fn Java_io_zenoh_jni_JNIQuery_replySuccessViaJNI( qos_priority: jint, qos_congestion_control: jint, ) { - let _ = || -> Result<()> { + let _ = || -> ZResult<()> { let query = Arc::from_raw(query_ptr); let key_expr = process_kotlin_key_expr(&mut env, &key_expr_str, key_expr_ptr)?; let payload = decode_byte_array(&env, payload)?; @@ -97,7 +97,7 @@ pub(crate) unsafe extern "C" fn Java_io_zenoh_jni_JNIQuery_replySuccessViaJNI( } else { reply_builder.congestion_control(CongestionControl::Drop) }; - reply_builder.wait().map_err(|err| session_error!(err)) + reply_builder.wait().map_err(|err| zerror!(err)) }() .map_err(|err| throw_exception!(env, err)); } @@ -129,14 +129,14 @@ pub(crate) unsafe extern "C" fn Java_io_zenoh_jni_JNIQuery_replyErrorViaJNI( encoding_id: jint, encoding_schema: /*nullable*/ JString, ) { - let _ = || -> Result<()> { + let _ = || -> ZResult<()> { let query = Arc::from_raw(query_ptr); let encoding = decode_encoding(&mut env, encoding_id, &encoding_schema)?; query .reply_err(decode_byte_array(&env, payload)?) .encoding(encoding) .wait() - .map_err(|err| session_error!(err)) + .map_err(|err| zerror!(err)) }() .map_err(|err| throw_exception!(env, err)); } @@ -178,7 +178,7 @@ pub(crate) unsafe extern "C" fn Java_io_zenoh_jni_JNIQuery_replyDeleteViaJNI( qos_priority: jint, qos_congestion_control: jint, ) { - let _ = || -> Result<()> { + let _ = || -> ZResult<()> { let query = Arc::from_raw(query_ptr); let key_expr = process_kotlin_key_expr(&mut env, &key_expr_str, key_expr_ptr)?; let mut reply_builder = query.reply_del(key_expr); @@ -196,7 +196,7 @@ pub(crate) unsafe extern "C" fn Java_io_zenoh_jni_JNIQuery_replyDeleteViaJNI( } else { reply_builder.congestion_control(CongestionControl::Drop) }; - reply_builder.wait().map_err(|err| session_error!(err)) + reply_builder.wait().map_err(|err| zerror!(err)) }() .map_err(|err| throw_exception!(env, err)); } diff --git a/zenoh-jni/src/scouting.rs b/zenoh-jni/src/scouting.rs index bf8e0874f..9791d5ce6 100644 --- a/zenoh-jni/src/scouting.rs +++ b/zenoh-jni/src/scouting.rs @@ -22,11 +22,8 @@ use jni::{ use zenoh::{config::WhatAmIMatcher, prelude::Wait}; use zenoh::{scouting::Scout, Config}; -use crate::{errors::Result, throw_exception}; -use crate::{ - session_error, - utils::{get_callback_global_ref, get_java_vm}, -}; +use crate::utils::{get_callback_global_ref, get_java_vm}; +use crate::{errors::ZResult, throw_exception, zerror}; /// Start a scout. /// @@ -47,7 +44,7 @@ pub unsafe extern "C" fn Java_io_zenoh_jni_JNIScout_00024Companion_scoutViaJNI( callback: JObject, config_ptr: /*nullable=*/ *const Config, ) -> *const Scout<()> { - || -> Result<*const Scout<()>> { + || -> ZResult<*const Scout<()>> { let callback_global_ref = get_callback_global_ref(&mut env, callback)?; let java_vm = Arc::new(get_java_vm(&mut env)?); let whatAmIMatcher: WhatAmIMatcher = (whatAmI as u8).try_into().unwrap(); // The validity of the operation is guaranteed on the kotlin layer. @@ -92,7 +89,7 @@ pub unsafe extern "C" fn Java_io_zenoh_jni_JNIScout_00024Companion_scoutViaJNI( }) .wait() .map(|scout| Arc::into_raw(Arc::new(scout))) - .map_err(|err| session_error!(err)) + .map_err(|err| zerror!(err)) }() .unwrap_or_else(|err| { throw_exception!(env, err); diff --git a/zenoh-jni/src/session.rs b/zenoh-jni/src/session.rs index 0231e8468..6707952f1 100644 --- a/zenoh-jni/src/session.rs +++ b/zenoh-jni/src/session.rs @@ -30,8 +30,7 @@ use zenoh::{ }; use crate::{ - errors::Result, jni_error, key_expr::process_kotlin_key_expr, session_error, throw_exception, - utils::*, + errors::ZResult, key_expr::process_kotlin_key_expr, throw_exception, utils::*, zerror, }; /// Open a Zenoh session via JNI. @@ -59,7 +58,7 @@ pub unsafe extern "C" fn Java_io_zenoh_jni_JNISession_openSessionViaJNI( Ok(session) => Arc::into_raw(Arc::new(session)), Err(err) => { tracing::error!("Unable to open session: {}", err); - throw_exception!(env, session_error!(err)); + throw_exception!(env, zerror!(err)); null() } } @@ -69,11 +68,11 @@ pub unsafe extern "C" fn Java_io_zenoh_jni_JNISession_openSessionViaJNI( /// /// If the config path provided is null then the default configuration is loaded. /// -unsafe fn open_session(config_ptr: *const Config) -> Result { +unsafe fn open_session(config_ptr: *const Config) -> ZResult { let config = Arc::from_raw(config_ptr); let result = zenoh::open(config.as_ref().clone()) .wait() - .map_err(|err| session_error!(err)); + .map_err(|err| zerror!(err)); mem::forget(config); result } @@ -103,7 +102,7 @@ pub extern "C" fn Java_io_zenoh_jni_JNISession_openSessionWithJsonConfigViaJNI( Ok(session) => Arc::into_raw(Arc::new(session)), Err(err) => { tracing::error!("Unable to open session: {}", err); - throw_exception!(env, session_error!(err)); + throw_exception!(env, zerror!(err)); null() } } @@ -111,17 +110,15 @@ pub extern "C" fn Java_io_zenoh_jni_JNISession_openSessionWithJsonConfigViaJNI( /// Open a Zenoh session with the provided json configuration. /// -fn open_session_with_json_config(env: &mut JNIEnv, json_config: JString) -> Result { +fn open_session_with_json_config(env: &mut JNIEnv, json_config: JString) -> ZResult { let json_config = decode_string(env, &json_config)?; let mut deserializer = - json5::Deserializer::from_str(&json_config).map_err(|err| session_error!(err))?; + json5::Deserializer::from_str(&json_config).map_err(|err| zerror!(err))?; let config = Config::from_deserializer(&mut deserializer).map_err(|err| match err { - Ok(c) => session_error!("Invalid configuration: {}", c), - Err(e) => session_error!("JSON error: {}", e), + Ok(c) => zerror!("Invalid configuration: {}", c), + Err(e) => zerror!("JSON error: {}", e), })?; - zenoh::open(config) - .wait() - .map_err(|err| session_error!(err)) + zenoh::open(config).wait().map_err(|err| zerror!(err)) } /// Open a Zenoh session with a YAML configuration. @@ -149,7 +146,7 @@ pub extern "C" fn Java_io_zenoh_jni_JNISession_openSessionWithYamlConfigViaJNI( Ok(session) => Arc::into_raw(Arc::new(session)), Err(err) => { tracing::error!("Unable to open session: {}", err); - throw_exception!(env, session_error!(err)); + throw_exception!(env, zerror!(err)); null() } } @@ -157,16 +154,14 @@ pub extern "C" fn Java_io_zenoh_jni_JNISession_openSessionWithYamlConfigViaJNI( /// Open a Zenoh session with the provided yaml configuration. /// -fn open_session_with_yaml_config(env: &mut JNIEnv, yaml_config: JString) -> Result { +fn open_session_with_yaml_config(env: &mut JNIEnv, yaml_config: JString) -> ZResult { let yaml_config = decode_string(env, &yaml_config)?; let deserializer = serde_yaml::Deserializer::from_str(&yaml_config); let config = Config::from_deserializer(deserializer).map_err(|err| match err { - Ok(c) => session_error!("Invalid configuration: {}", c), - Err(e) => session_error!("YAML error: {}", e), + Ok(c) => zerror!("Invalid configuration: {}", c), + Err(e) => zerror!("YAML error: {}", e), })?; - zenoh::open(config) - .wait() - .map_err(|err| session_error!(err)) + zenoh::open(config).wait().map_err(|err| zerror!(err)) } /// Closes a Zenoh session via JNI. @@ -231,7 +226,7 @@ pub unsafe extern "C" fn Java_io_zenoh_jni_JNISession_declarePublisherViaJNI( reliability: jint, ) -> *const Publisher<'static> { let session = Arc::from_raw(session_ptr); - let publisher_ptr = || -> Result<*const Publisher<'static>> { + let publisher_ptr = || -> ZResult<*const Publisher<'static>> { let key_expr = process_kotlin_key_expr(&mut env, &key_expr_str, key_expr_ptr)?; let congestion_control = decode_congestion_control(congestion_control)?; let priority = decode_priority(priority)?; @@ -245,7 +240,7 @@ pub unsafe extern "C" fn Java_io_zenoh_jni_JNISession_declarePublisherViaJNI( .wait(); match result { Ok(publisher) => Ok(Arc::into_raw(Arc::new(publisher))), - Err(err) => Err(session_error!(err)), + Err(err) => Err(zerror!(err)), } }() .unwrap_or_else(|err| { @@ -300,7 +295,7 @@ pub unsafe extern "C" fn Java_io_zenoh_jni_JNISession_putViaJNI( reliability: jint, ) { let session = Arc::from_raw(session_ptr); - let _ = || -> Result<()> { + let _ = || -> ZResult<()> { let key_expr = process_kotlin_key_expr(&mut env, &key_expr_str, key_expr_ptr)?; let payload = decode_byte_array(&env, payload)?; let encoding = decode_encoding(&mut env, encoding_id, &encoding_schema)?; @@ -324,7 +319,7 @@ pub unsafe extern "C" fn Java_io_zenoh_jni_JNISession_putViaJNI( put_builder .wait() .map(|_| tracing::trace!("Put on '{key_expr}'")) - .map_err(|err| session_error!(err)) + .map_err(|err| zerror!(err)) }() .map_err(|err| throw_exception!(env, err)); std::mem::forget(session); @@ -369,7 +364,7 @@ pub unsafe extern "C" fn Java_io_zenoh_jni_JNISession_deleteViaJNI( reliability: jint, ) { let session = Arc::from_raw(session_ptr); - let _ = || -> Result<()> { + let _ = || -> ZResult<()> { let key_expr = process_kotlin_key_expr(&mut env, &key_expr_str, key_expr_ptr)?; let congestion_control = decode_congestion_control(congestion_control)?; let priority = decode_priority(priority)?; @@ -390,7 +385,7 @@ pub unsafe extern "C" fn Java_io_zenoh_jni_JNISession_deleteViaJNI( delete_builder .wait() .map(|_| tracing::trace!("Delete on '{key_expr}'")) - .map_err(|err| session_error!(err)) + .map_err(|err| zerror!(err)) }() .map_err(|err| throw_exception!(env, err)); std::mem::forget(session); @@ -433,7 +428,7 @@ pub unsafe extern "C" fn Java_io_zenoh_jni_JNISession_declareSubscriberViaJNI( on_close: JObject, ) -> *const Subscriber<()> { let session = Arc::from_raw(session_ptr); - || -> Result<*const Subscriber<()>> { + || -> ZResult<*const Subscriber<()>> { let java_vm = Arc::new(get_java_vm(&mut env)?); let callback_global_ref = get_callback_global_ref(&mut env, callback)?; let on_close_global_ref = get_callback_global_ref(&mut env, on_close)?; @@ -446,9 +441,9 @@ pub unsafe extern "C" fn Java_io_zenoh_jni_JNISession_declareSubscriberViaJNI( .declare_subscriber(key_expr.to_owned()) .with(move |sample: Sample| { on_close.noop(); // Moves `on_close` inside the closure so it gets destroyed with the closure - let _ = || -> Result<()> { + let _ = || -> ZResult<()> { let mut env = java_vm.attach_current_thread_as_daemon().map_err(|err| { - jni_error!("Unable to attach thread for subscriber: {}", err) + zerror!("Unable to attach thread for subscriber: {}", err) })?; let byte_array = bytes_to_java_array(&env, sample.payload()) .map(|array| env.auto_local(array))?; @@ -471,12 +466,12 @@ pub unsafe extern "C" fn Java_io_zenoh_jni_JNISession_declareSubscriberViaJNI( |attachment| bytes_to_java_array(&env, attachment), ) .map(|array| env.auto_local(array)) - .map_err(|err| jni_error!("Error processing attachment: {}", err))?; + .map_err(|err| zerror!("Error processing attachment: {}", err))?; - let key_expr_str = - env.auto_local(env.new_string(sample.key_expr().to_string()).map_err( - |err| jni_error!("Error processing sample key expr: {}", err), - )?); + let key_expr_str = env.auto_local( + env.new_string(sample.key_expr().to_string()) + .map_err(|err| zerror!("Error processing sample key expr: {}", err))?, + ); let express = sample.express(); let priority = sample.priority() as jint; @@ -500,15 +495,14 @@ pub unsafe extern "C" fn Java_io_zenoh_jni_JNISession_declareSubscriberViaJNI( JValue::from(cc), ], ) - .map_err(|err| jni_error!(err))?; + .map_err(|err| zerror!(err))?; Ok(()) }() .map_err(|err| tracing::error!("On subscriber callback error: {err}")); }) .wait(); - let subscriber = - result.map_err(|err| session_error!("Unable to declare subscriber: {}", err))?; + let subscriber = result.map_err(|err| zerror!("Unable to declare subscriber: {}", err))?; tracing::debug!("Subscriber declared on '{}'.", key_expr); std::mem::forget(session); @@ -561,7 +555,7 @@ pub unsafe extern "C" fn Java_io_zenoh_jni_JNISession_declareQueryableViaJNI( complete: jboolean, ) -> *const Queryable<()> { let session = Arc::from_raw(session_ptr); - let query_ptr = || -> Result<*const Queryable<()>> { + let query_ptr = || -> ZResult<*const Queryable<()>> { let java_vm = Arc::new(get_java_vm(&mut env)?); let callback_global_ref = get_callback_global_ref(&mut env, callback)?; let on_close_global_ref = get_callback_global_ref(&mut env, on_close)?; @@ -591,7 +585,7 @@ pub unsafe extern "C" fn Java_io_zenoh_jni_JNISession_declareQueryableViaJNI( let queryable = builder .wait() - .map_err(|err| session_error!("Error declaring queryable: {}", err))?; + .map_err(|err| zerror!("Error declaring queryable: {}", err))?; Ok(Arc::into_raw(Arc::new(queryable))) }() .unwrap_or_else(|err| { @@ -602,12 +596,12 @@ pub unsafe extern "C" fn Java_io_zenoh_jni_JNISession_declareQueryableViaJNI( query_ptr } -fn on_query(mut env: JNIEnv, query: Query, callback_global_ref: &GlobalRef) -> Result<()> { +fn on_query(mut env: JNIEnv, query: Query, callback_global_ref: &GlobalRef) -> ZResult<()> { let selector_params_jstr = env .new_string(query.parameters().to_string()) .map(|value| env.auto_local(value)) .map_err(|err| { - jni_error!( + zerror!( "Could not create a JString through JNI for the Query key expression. {}", err ) @@ -640,13 +634,13 @@ fn on_query(mut env: JNIEnv, query: Query, callback_global_ref: &GlobalRef) -> R |attachment| bytes_to_java_array(&env, attachment), ) .map(|value| env.auto_local(value)) - .map_err(|err| jni_error!("Error processing attachment of reply: {}.", err))?; + .map_err(|err| zerror!("Error processing attachment of reply: {}.", err))?; let key_expr_str = env .new_string(&query.key_expr().to_string()) .map(|key_expr| env.auto_local(key_expr)) .map_err(|err| { - jni_error!( + zerror!( "Could not create a JString through JNI for the Query key expression: {}.", err ) @@ -679,7 +673,7 @@ fn on_query(mut env: JNIEnv, query: Query, callback_global_ref: &GlobalRef) -> R Arc::from_raw(query_ptr); }; _ = env.exception_describe(); - jni_error!(err) + zerror!(err) }); result } @@ -711,13 +705,13 @@ pub unsafe extern "C" fn Java_io_zenoh_jni_JNISession_declareKeyExprViaJNI( key_expr_str: JString, ) -> *const KeyExpr<'static> { let session: Arc = Arc::from_raw(session_ptr); - let key_expr_ptr = || -> Result<*const KeyExpr<'static>> { + let key_expr_ptr = || -> ZResult<*const KeyExpr<'static>> { let key_expr_str = decode_string(&mut env, &key_expr_str)?; let key_expr = session .declare_keyexpr(key_expr_str.to_owned()) .wait() .map_err(|err| { - session_error!( + zerror!( "Unable to declare key expression '{}': {}", key_expr_str, err @@ -769,7 +763,7 @@ pub unsafe extern "C" fn Java_io_zenoh_jni_JNISession_undeclareKeyExprViaJNI( Err(err) => { throw_exception!( env, - session_error!("Unable to declare key expression '{}': {}", key_expr, err) + zerror!("Unable to declare key expression '{}': {}", key_expr, err) ); } } @@ -828,7 +822,7 @@ pub unsafe extern "C" fn Java_io_zenoh_jni_JNISession_getViaJNI( encoding_schema: /*nullable*/ JString, ) { let session = Arc::from_raw(session_ptr); - let _ = || -> Result<()> { + let _ = || -> ZResult<()> { let key_expr = process_kotlin_key_expr(&mut env, &key_expr_str, key_expr_ptr)?; let java_vm = Arc::new(get_java_vm(&mut env)?); let callback_global_ref = get_callback_global_ref(&mut env, callback)?; @@ -846,11 +840,11 @@ pub unsafe extern "C" fn Java_io_zenoh_jni_JNISession_getViaJNI( let mut get_builder = session .get(selector) .callback(move |reply| { - || -> Result<()> { + || -> ZResult<()> { on_close.noop(); // Does nothing, but moves `on_close` inside the closure so it gets destroyed with the closure tracing::debug!("Receiving reply through JNI: {:?}", reply); let mut env = java_vm.attach_current_thread_as_daemon().map_err(|err| { - jni_error!("Unable to attach thread for GET query callback: {}.", err) + zerror!("Unable to attach thread for GET query callback: {}.", err) })?; match reply.result() { @@ -888,7 +882,7 @@ pub unsafe extern "C" fn Java_io_zenoh_jni_JNISession_getViaJNI( get_builder .wait() .map(|_| tracing::trace!("Performing get on '{key_expr}'.",)) - .map_err(|err| session_error!(err)) + .map_err(|err| zerror!(err)) }() .map_err(|err| throw_exception!(env, err)); std::mem::forget(session); @@ -899,13 +893,13 @@ fn on_reply_success( replier_id: Option, sample: &Sample, callback_global_ref: &GlobalRef, -) -> Result<()> { +) -> ZResult<()> { let zenoh_id = replier_id .map_or_else( || Ok(JByteArray::default()), |replier_id| { env.byte_array_from_slice(&replier_id.to_le_bytes()) - .map_err(|err| jni_error!(err)) + .map_err(|err| zerror!(err)) }, ) .map(|value| env.auto_local(value))?; @@ -935,13 +929,13 @@ fn on_reply_success( |attachment| bytes_to_java_array(env, attachment), ) .map(|value| env.auto_local(value)) - .map_err(|err| jni_error!("Error processing attachment of reply: {}.", err))?; + .map_err(|err| zerror!("Error processing attachment of reply: {}.", err))?; let key_expr_str = env .new_string(sample.key_expr().to_string()) .map(|value| env.auto_local(value)) .map_err(|err| { - jni_error!( + zerror!( "Could not create a JString through JNI for the Sample key expression. {}", err ) @@ -974,7 +968,7 @@ fn on_reply_success( Ok(_) => Ok(()), Err(err) => { _ = env.exception_describe(); - Err(jni_error!("On GET callback error: {}", err)) + Err(zerror!("On GET callback error: {}", err)) } }; result @@ -985,13 +979,13 @@ fn on_reply_error( replier_id: Option, reply_error: &ReplyError, callback_global_ref: &GlobalRef, -) -> Result<()> { +) -> ZResult<()> { let zenoh_id = replier_id .map_or_else( || Ok(JByteArray::default()), |replier_id| { env.byte_array_from_slice(&replier_id.to_le_bytes()) - .map_err(|err| jni_error!(err)) + .map_err(|err| zerror!(err)) }, ) .map(|value| env.auto_local(value))?; @@ -1031,7 +1025,7 @@ fn on_reply_error( Ok(_) => Ok(()), Err(err) => { _ = env.exception_describe(); - Err(jni_error!("On GET callback error: {}", err)) + Err(zerror!("On GET callback error: {}", err)) } }; result diff --git a/zenoh-jni/src/utils.rs b/zenoh-jni/src/utils.rs index fd0583d92..36ed7449d 100644 --- a/zenoh-jni/src/utils.rs +++ b/zenoh-jni/src/utils.rs @@ -14,7 +14,7 @@ use std::sync::Arc; -use crate::{errors::Result, jni_error, session_error, throw_exception}; +use crate::{errors::ZResult, throw_exception, zerror}; use jni::{ objects::{JByteArray, JObject, JString}, sys::jint, @@ -28,13 +28,13 @@ use zenoh::{ }; /// Converts a JString into a rust String. -pub(crate) fn decode_string(env: &mut JNIEnv, string: &JString) -> Result { +pub(crate) fn decode_string(env: &mut JNIEnv, string: &JString) -> ZResult { let binding = env .get_string(string) - .map_err(|err| jni_error!("Error while retrieving JString: {}", err))?; + .map_err(|err| zerror!("Error while retrieving JString: {}", err))?; let value = binding .to_str() - .map_err(|err| jni_error!("Error decoding JString: {}", err))?; + .map_err(|err| zerror!("Error decoding JString: {}", err))?; Ok(value.to_string()) } @@ -42,99 +42,98 @@ pub(crate) fn decode_encoding( env: &mut JNIEnv, encoding: jint, schema: &JString, -) -> Result { +) -> ZResult { let schema: Option = if schema.is_null() { None } else { Some(decode_string(env, schema)?.into_bytes().into()) }; let encoding_id = - u16::try_from(encoding).map_err(|err| jni_error!("Failed to decode encoding: {}", err))?; + u16::try_from(encoding).map_err(|err| zerror!("Failed to decode encoding: {}", err))?; Ok(Encoding::new(encoding_id, schema)) } -pub(crate) fn get_java_vm(env: &mut JNIEnv) -> Result { +pub(crate) fn get_java_vm(env: &mut JNIEnv) -> ZResult { env.get_java_vm() - .map_err(|err| jni_error!("Unable to retrieve JVM reference: {}", err)) + .map_err(|err| zerror!("Unable to retrieve JVM reference: {}", err)) } pub(crate) fn get_callback_global_ref( env: &mut JNIEnv, callback: JObject, -) -> crate::errors::Result { +) -> crate::errors::ZResult { env.new_global_ref(callback) - .map_err(|err| jni_error!("Unable to get reference to the provided callback: {}", err)) + .map_err(|err| zerror!("Unable to get reference to the provided callback: {}", err)) } /// Helper function to convert a JByteArray into a Vec. -pub(crate) fn decode_byte_array(env: &JNIEnv<'_>, payload: JByteArray) -> Result> { +pub(crate) fn decode_byte_array(env: &JNIEnv<'_>, payload: JByteArray) -> ZResult> { let payload_len = env .get_array_length(&payload) .map(|length| length as usize) - .map_err(|err| jni_error!(err))?; + .map_err(|err| zerror!(err))?; let mut buff = vec![0; payload_len]; env.get_byte_array_region(payload, 0, &mut buff[..]) - .map_err(|err| jni_error!(err))?; + .map_err(|err| zerror!(err))?; let buff: Vec = unsafe { std::mem::transmute::, Vec>(buff) }; Ok(buff) } -pub(crate) fn decode_priority(priority: jint) -> Result { - Priority::try_from(priority as u8) - .map_err(|err| session_error!("Error retrieving priority: {}.", err)) +pub(crate) fn decode_priority(priority: jint) -> ZResult { + Priority::try_from(priority as u8).map_err(|err| zerror!("Error retrieving priority: {}.", err)) } -pub(crate) fn decode_congestion_control(congestion_control: jint) -> Result { +pub(crate) fn decode_congestion_control(congestion_control: jint) -> ZResult { match congestion_control { 1 => Ok(CongestionControl::Block), 0 => Ok(CongestionControl::Drop), - value => Err(session_error!("Unknown congestion control '{}'.", value)), + value => Err(zerror!("Unknown congestion control '{}'.", value)), } } -pub(crate) fn decode_query_target(target: jint) -> Result { +pub(crate) fn decode_query_target(target: jint) -> ZResult { match target { 0 => Ok(QueryTarget::BestMatching), 1 => Ok(QueryTarget::All), 2 => Ok(QueryTarget::AllComplete), - value => Err(session_error!("Unable to decode QueryTarget '{}'.", value)), + value => Err(zerror!("Unable to decode QueryTarget '{}'.", value)), } } -pub(crate) fn decode_consolidation(consolidation: jint) -> Result { +pub(crate) fn decode_consolidation(consolidation: jint) -> ZResult { match consolidation { 0 => Ok(ConsolidationMode::Auto), 1 => Ok(ConsolidationMode::None), 2 => Ok(ConsolidationMode::Monotonic), 3 => Ok(ConsolidationMode::Latest), - value => Err(session_error!("Unable to decode consolidation '{}'", value)), + value => Err(zerror!("Unable to decode consolidation '{}'", value)), } } -pub(crate) fn decode_reliability(reliability: jint) -> Result { +pub(crate) fn decode_reliability(reliability: jint) -> ZResult { match reliability { 0 => Ok(Reliability::BestEffort), 1 => Ok(Reliability::Reliable), - value => Err(session_error!("Unable to decode reliability '{}'", value)), + value => Err(zerror!("Unable to decode reliability '{}'", value)), } } -pub(crate) fn bytes_to_java_array<'a>(env: &JNIEnv<'a>, slice: &ZBytes) -> Result> { +pub(crate) fn bytes_to_java_array<'a>(env: &JNIEnv<'a>, slice: &ZBytes) -> ZResult> { env.byte_array_from_slice( slice .deserialize::>() - .map_err(|err| session_error!("Unable to deserialize slice: {}", err))? + .map_err(|err| zerror!("Unable to deserialize slice: {}", err))? .as_ref(), ) - .map_err(|err| jni_error!(err)) + .map_err(|err| zerror!(err)) } -pub(crate) fn slice_to_java_string<'a>(env: &JNIEnv<'a>, slice: &ZSlice) -> Result> { +pub(crate) fn slice_to_java_string<'a>(env: &JNIEnv<'a>, slice: &ZSlice) -> ZResult> { env.new_string( String::from_utf8(slice.to_vec()) - .map_err(|err| session_error!("Unable to decode string: {}", err))?, + .map_err(|err| zerror!("Unable to decode string: {}", err))?, ) - .map_err(|err| jni_error!(err)) + .map_err(|err| zerror!(err)) } /// A type that calls a function when dropped @@ -178,7 +177,7 @@ pub(crate) fn load_on_close( _ = env.exception_describe(); throw_exception!( env, - jni_error!("Error while running 'onClose' callback: {}", err) + zerror!("Error while running 'onClose' callback: {}", err) ); } } diff --git a/zenoh-jni/src/zbytes.rs b/zenoh-jni/src/zbytes.rs index fff9a25df..aebf43fc2 100644 --- a/zenoh-jni/src/zbytes.rs +++ b/zenoh-jni/src/zbytes.rs @@ -21,7 +21,7 @@ use jni::{ }; use zenoh::bytes::ZBytes; -use crate::{errors::Result, jni_error, utils::bytes_to_java_array, zerror}; +use crate::{errors::ZResult, utils::bytes_to_java_array, zerror}; use crate::{throw_exception, utils::decode_byte_array}; /// @@ -44,8 +44,8 @@ pub extern "C" fn Java_io_zenoh_jni_JNIZBytes_serializeIntoMapViaJNI( _class: JClass, map: JObject, ) -> jbyteArray { - || -> Result { - let zbytes = java_map_to_zbytes(&mut env, &map).map_err(|err| jni_error!(err))?; + || -> ZResult { + let zbytes = java_map_to_zbytes(&mut env, &map).map_err(|err| zerror!(err))?; let byte_array = bytes_to_java_array(&env, &zbytes)?; Ok(byte_array.as_raw()) }() @@ -84,13 +84,13 @@ pub extern "C" fn Java_io_zenoh_jni_JNIZBytes_deserializeIntoMapViaJNI( _class: JClass, serialized_map: JByteArray, ) -> jobject { - || -> Result { + || -> ZResult { let payload = decode_byte_array(&env, serialized_map)?; let zbytes = ZBytes::new(payload); let deserialization: HashMap, Vec> = zbytes .deserialize::, Vec>>() .map_err(|err| zerror!(err))?; - hashmap_to_java_map(&mut env, &deserialization).map_err(|err| jni_error!(err)) + hashmap_to_java_map(&mut env, &deserialization).map_err(|err| zerror!(err)) }() .unwrap_or_else(|err| { throw_exception!(env, err); @@ -134,8 +134,8 @@ pub extern "C" fn Java_io_zenoh_jni_JNIZBytes_serializeIntoListViaJNI( _class: JClass, list: JObject, ) -> jbyteArray { - || -> Result { - let zbytes = java_list_to_zbytes(&mut env, &list).map_err(|err| jni_error!(err))?; + || -> ZResult { + let zbytes = java_list_to_zbytes(&mut env, &list).map_err(|err| zerror!(err))?; let byte_array = bytes_to_java_array(&env, &zbytes)?; Ok(byte_array.as_raw()) }() @@ -175,10 +175,10 @@ pub extern "C" fn Java_io_zenoh_jni_JNIZBytes_deserializeIntoListViaJNI( _class: JClass, serialized_list: JByteArray, ) -> jobject { - || -> Result { + || -> ZResult { let payload = decode_byte_array(&env, serialized_list)?; let zbytes = ZBytes::new(payload); - zbytes_to_java_list(&mut env, &zbytes).map_err(|err| jni_error!(err)) + zbytes_to_java_list(&mut env, &zbytes).map_err(|err| zerror!(err)) }() .unwrap_or_else(|err| { throw_exception!(env, err); diff --git a/zenoh-jni/src/zenoh_id.rs b/zenoh-jni/src/zenoh_id.rs index 192131ae5..2067842fb 100644 --- a/zenoh-jni/src/zenoh_id.rs +++ b/zenoh-jni/src/zenoh_id.rs @@ -12,7 +12,7 @@ // ZettaScale Zenoh Team, // -use crate::{errors::Result, jni_error, throw_exception, utils::decode_byte_array, zerror}; +use crate::{errors::ZResult, throw_exception, utils::decode_byte_array, zerror}; use jni::{ objects::{JByteArray, JClass, JString}, sys::jstring, @@ -28,11 +28,11 @@ pub extern "C" fn Java_io_zenoh_jni_JNIZenohID_toStringViaJNI( _class: JClass, zenoh_id: JByteArray, ) -> jstring { - || -> Result { + || -> ZResult { let bytes = decode_byte_array(&env, zenoh_id)?; let zenohid = ZenohId::try_from(bytes.as_slice()).map_err(|err| zerror!(err))?; env.new_string(zenohid.to_string()) - .map_err(|err| jni_error!(err)) + .map_err(|err| zerror!(err)) }() .unwrap_or_else(|err| { throw_exception!(env, err); diff --git a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/Session.kt b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/Session.kt index dc69eb350..bc4964e0a 100644 --- a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/Session.kt +++ b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/Session.kt @@ -14,7 +14,7 @@ package io.zenoh -import io.zenoh.exceptions.SessionException +import io.zenoh.exceptions.ZError import io.zenoh.handlers.Callback import io.zenoh.handlers.ChannelHandler import io.zenoh.handlers.Handler @@ -62,7 +62,7 @@ class Session private constructor(private val config: Config) : AutoCloseable { companion object { - private val sessionClosedException = SessionException("Session is closed.") + private val sessionClosedException = ZError("Session is closed.") /** * Open a [Session] with the provided [Config]. diff --git a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/exceptions/JNIException.kt b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/exceptions/JNIException.kt deleted file mode 100644 index e29e117c5..000000000 --- a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/exceptions/JNIException.kt +++ /dev/null @@ -1,23 +0,0 @@ -// -// Copyright (c) 2023 ZettaScale Technology -// -// This program and the accompanying materials are made available under the -// terms of the Eclipse Public License 2.0 which is available at -// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 -// which is available at https://www.apache.org/licenses/LICENSE-2.0. -// -// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 -// -// Contributors: -// ZettaScale Zenoh Team, -// - -package io.zenoh.exceptions - -/** - * JNI (Java native interface) exception. - * - * This type of exception is thrown from the native code when something goes wrong regarding the - * communication between the Java/Kotlin layer and the native layer through the JNI. - */ -class JNIException(msg: String?) : ZError(msg) diff --git a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/exceptions/KeyExprException.kt b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/exceptions/KeyExprException.kt deleted file mode 100644 index 9ea4226d2..000000000 --- a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/exceptions/KeyExprException.kt +++ /dev/null @@ -1,24 +0,0 @@ -// -// Copyright (c) 2023 ZettaScale Technology -// -// This program and the accompanying materials are made available under the -// terms of the Eclipse Public License 2.0 which is available at -// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 -// which is available at https://www.apache.org/licenses/LICENSE-2.0. -// -// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 -// -// Contributors: -// ZettaScale Zenoh Team, -// - -package io.zenoh.exceptions - -/** - * Key expression exception. - * - * This kind of exception is thrown from the native code when something goes wrong regarding a key expression, - * for instance when attempting to create a [io.zenoh.keyexpr.KeyExpr] from a string that does not respect the - * key expression conventions. - */ -class KeyExprException(msg: String?) : ZError(msg) diff --git a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/exceptions/SessionException.kt b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/exceptions/SessionException.kt deleted file mode 100644 index 9713c4c56..000000000 --- a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/exceptions/SessionException.kt +++ /dev/null @@ -1,22 +0,0 @@ -// -// Copyright (c) 2023 ZettaScale Technology -// -// This program and the accompanying materials are made available under the -// terms of the Eclipse Public License 2.0 which is available at -// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 -// which is available at https://www.apache.org/licenses/LICENSE-2.0. -// -// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 -// -// Contributors: -// ZettaScale Zenoh Team, -// - -package io.zenoh.exceptions - -/** - * Session exception. - * - * This kind of exception is thrown from the native code when something goes wrong with a Zenoh [io.zenoh.Session]. - */ -class SessionException(message: String?) : ZError(message) diff --git a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/exceptions/ZError.kt b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/exceptions/ZError.kt index 59519379a..c63a19ce0 100644 --- a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/exceptions/ZError.kt +++ b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/exceptions/ZError.kt @@ -17,4 +17,4 @@ package io.zenoh.exceptions /** * A Zenoh Error. */ -open class ZError(override val message: String? = null): Exception() +class ZError(override val message: String? = null): Exception() diff --git a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/jni/JNISession.kt b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/jni/JNISession.kt index 3c2bfb5bd..c88b3db81 100644 --- a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/jni/JNISession.kt +++ b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/jni/JNISession.kt @@ -16,7 +16,6 @@ package io.zenoh.jni import io.zenoh.* import io.zenoh.prelude.Encoding -import io.zenoh.exceptions.SessionException import io.zenoh.exceptions.ZError import io.zenoh.handlers.Callback import io.zenoh.jni.callbacks.JNIOnCloseCallback @@ -212,7 +211,7 @@ internal class JNISession { keyExpr.jniKeyExpr?.run { undeclareKeyExprViaJNI(sessionPtr.get(), this.ptr) keyExpr.jniKeyExpr = null - } ?: throw SessionException("Attempting to undeclare a non declared key expression.") + } ?: throw ZError("Attempting to undeclare a non declared key expression.") } @Throws(ZError::class) diff --git a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/keyexpr/IntoKeyExpr.kt b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/keyexpr/IntoKeyExpr.kt index 7eae088d6..cbb5cd842 100644 --- a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/keyexpr/IntoKeyExpr.kt +++ b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/keyexpr/IntoKeyExpr.kt @@ -14,12 +14,11 @@ package io.zenoh.keyexpr -import io.zenoh.exceptions.KeyExprException +import io.zenoh.exceptions.ZError fun String.intoKeyExpr(): Result = runCatching { if (this.isEmpty()) { - return Result.failure(KeyExprException("Attempting to create a KeyExpr from an empty string.")) + return Result.failure(ZError("Attempting to create a KeyExpr from an empty string.")) } return KeyExpr.autocanonize(this) } - diff --git a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/publication/Publisher.kt b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/publication/Publisher.kt index 5746d2fdc..6442e8747 100644 --- a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/publication/Publisher.kt +++ b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/publication/Publisher.kt @@ -15,7 +15,7 @@ package io.zenoh.publication import io.zenoh.* -import io.zenoh.exceptions.SessionException +import io.zenoh.exceptions.ZError import io.zenoh.jni.JNIPublisher import io.zenoh.keyexpr.KeyExpr import io.zenoh.prelude.Encoding @@ -69,7 +69,7 @@ class Publisher internal constructor( ) : SessionDeclaration, AutoCloseable { companion object { - private val InvalidPublisherResult = Result.failure(SessionException("Publisher is not valid.")) + private val InvalidPublisherResult = Result.failure(ZError("Publisher is not valid.")) } val congestionControl = qos.congestionControl diff --git a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/queryable/Query.kt b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/queryable/Query.kt index 1b1b8e8f3..fc21a2845 100644 --- a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/queryable/Query.kt +++ b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/queryable/Query.kt @@ -16,7 +16,7 @@ package io.zenoh.queryable import io.zenoh.ZenohType import io.zenoh.selector.Selector -import io.zenoh.exceptions.SessionException +import io.zenoh.exceptions.ZError import io.zenoh.jni.JNIQuery import io.zenoh.keyexpr.KeyExpr import io.zenoh.prelude.Encoding @@ -80,7 +80,7 @@ class Query internal constructor( val result = it.replySuccess(sample) jniQuery = null result - } ?: Result.failure(SessionException("Query is invalid")) + } ?: Result.failure(ZError("Query is invalid")) } /** @@ -97,7 +97,7 @@ class Query internal constructor( val result = it.replyError(error, encoding) jniQuery = null result - } ?: Result.failure(SessionException("Query is invalid")) + } ?: Result.failure(ZError("Query is invalid")) } /** @@ -122,7 +122,7 @@ class Query internal constructor( val result = it.replyDelete(keyExpr, timestamp, attachment, qos) jniQuery = null result - } ?: Result.failure(SessionException("Query is invalid")) + } ?: Result.failure(ZError("Query is invalid")) } override fun close() { diff --git a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/selector/IntoSelector.kt b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/selector/IntoSelector.kt index 4ab68307c..d620610ad 100644 --- a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/selector/IntoSelector.kt +++ b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/selector/IntoSelector.kt @@ -14,12 +14,12 @@ package io.zenoh.selector -import io.zenoh.exceptions.KeyExprException +import io.zenoh.exceptions.ZError import io.zenoh.keyexpr.KeyExpr fun String.intoSelector(): Result = runCatching { if (this.isEmpty()) { - throw KeyExprException("Attempting to create a KeyExpr from an empty string.") + throw ZError("Attempting to create a KeyExpr from an empty string.") } val result = this.split('?', limit = 2) val keyExpr = KeyExpr.autocanonize(result[0]).getOrThrow() @@ -27,4 +27,3 @@ fun String.intoSelector(): Result = runCatching { Selector(keyExpr, params) } - diff --git a/zenoh-kotlin/src/commonTest/kotlin/io/zenoh/ConfigTest.kt b/zenoh-kotlin/src/commonTest/kotlin/io/zenoh/ConfigTest.kt index 7da196998..1420aeeb6 100644 --- a/zenoh-kotlin/src/commonTest/kotlin/io/zenoh/ConfigTest.kt +++ b/zenoh-kotlin/src/commonTest/kotlin/io/zenoh/ConfigTest.kt @@ -13,7 +13,6 @@ // package io.zenoh -import io.zenoh.exceptions.SessionException import io.zenoh.exceptions.ZError import io.zenoh.keyexpr.intoKeyExpr import io.zenoh.protocol.into diff --git a/zenoh-kotlin/src/commonTest/kotlin/io/zenoh/KeyExprTest.kt b/zenoh-kotlin/src/commonTest/kotlin/io/zenoh/KeyExprTest.kt index 6753adc76..87eadbabf 100644 --- a/zenoh-kotlin/src/commonTest/kotlin/io/zenoh/KeyExprTest.kt +++ b/zenoh-kotlin/src/commonTest/kotlin/io/zenoh/KeyExprTest.kt @@ -14,7 +14,6 @@ package io.zenoh -import io.zenoh.exceptions.SessionException import io.zenoh.keyexpr.KeyExpr import io.zenoh.keyexpr.SetIntersectionLevel import io.zenoh.keyexpr.intoKeyExpr @@ -116,13 +115,11 @@ class KeyExprTest { // Undeclaring twice a key expression shall fail. val undeclare2 = session.undeclare(keyExpr) assertTrue(undeclare2.isFailure) - assertTrue(undeclare2.exceptionOrNull() is SessionException) // Undeclaring a key expr that was not declared through a session. val keyExpr2 = "x/y/z".intoKeyExpr().getOrThrow() val undeclare3 = session.undeclare(keyExpr2) assertTrue(undeclare3.isFailure) - assertTrue(undeclare3.exceptionOrNull() is SessionException) session.close() keyExpr.close() diff --git a/zenoh-kotlin/src/commonTest/kotlin/io/zenoh/SelectorTest.kt b/zenoh-kotlin/src/commonTest/kotlin/io/zenoh/SelectorTest.kt index eeac10126..f13bd2cd8 100644 --- a/zenoh-kotlin/src/commonTest/kotlin/io/zenoh/SelectorTest.kt +++ b/zenoh-kotlin/src/commonTest/kotlin/io/zenoh/SelectorTest.kt @@ -1,6 +1,6 @@ package io.zenoh -import io.zenoh.exceptions.KeyExprException +import io.zenoh.exceptions.ZError import io.zenoh.selector.Selector import io.zenoh.selector.intoSelector import org.junit.jupiter.api.Assertions.assertNull @@ -22,7 +22,7 @@ class SelectorTest { assertNull(selector.parameters) } - assertFailsWith { "".intoSelector().getOrThrow() } + assertFailsWith { "".intoSelector().getOrThrow() } } @Test diff --git a/zenoh-kotlin/src/commonTest/kotlin/io/zenoh/SessionTest.kt b/zenoh-kotlin/src/commonTest/kotlin/io/zenoh/SessionTest.kt index 742451e68..0ff33aa49 100644 --- a/zenoh-kotlin/src/commonTest/kotlin/io/zenoh/SessionTest.kt +++ b/zenoh-kotlin/src/commonTest/kotlin/io/zenoh/SessionTest.kt @@ -14,11 +14,10 @@ package io.zenoh -import io.zenoh.exceptions.SessionException +import io.zenoh.exceptions.ZError import io.zenoh.keyexpr.KeyExpr import io.zenoh.keyexpr.intoKeyExpr import kotlinx.coroutines.runBlocking -import java.nio.file.Path import kotlin.test.* class SessionTest { @@ -73,9 +72,9 @@ class SessionTest { fun sessionClose_newDeclarationsReturnNullAfterClosingSession() { val session = Session.open(Config.default()).getOrThrow() session.close() - assertFailsWith { session.declarePublisher(testKeyExpr).getOrThrow() } - assertFailsWith { session.declareSubscriber(testKeyExpr, callback = {}).getOrThrow() } - assertFailsWith { session.declareQueryable(testKeyExpr, callback = {}).getOrThrow() } + assertFailsWith { session.declarePublisher(testKeyExpr).getOrThrow() } + assertFailsWith { session.declareSubscriber(testKeyExpr, callback = {}).getOrThrow() } + assertFailsWith { session.declareQueryable(testKeyExpr, callback = {}).getOrThrow() } } } From 185b41f82bc053b2e14459a10daaaf4c312c0eb0 Mon Sep 17 00:00:00 2001 From: Darius Maitia Date: Thu, 19 Sep 2024 10:58:03 -0300 Subject: [PATCH 6/7] ZError: cargo fmt --- zenoh-jni/src/publisher.rs | 5 +++-- zenoh-jni/src/query.rs | 4 +--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/zenoh-jni/src/publisher.rs b/zenoh-jni/src/publisher.rs index 91219057a..ead60c3f6 100644 --- a/zenoh-jni/src/publisher.rs +++ b/zenoh-jni/src/publisher.rs @@ -21,11 +21,12 @@ use jni::{ }; use zenoh::{pubsub::Publisher, Wait}; +use crate::throw_exception; use crate::{ errors::ZResult, - utils::{decode_byte_array, decode_encoding}, zerror, + utils::{decode_byte_array, decode_encoding}, + zerror, }; -use crate::{throw_exception}; /// Performs a PUT operation on a Zenoh publisher via JNI. /// diff --git a/zenoh-jni/src/query.rs b/zenoh-jni/src/query.rs index 4e1da0e69..bc28a2970 100644 --- a/zenoh-jni/src/query.rs +++ b/zenoh-jni/src/query.rs @@ -14,11 +14,9 @@ use std::sync::Arc; +use crate::utils::{decode_byte_array, decode_encoding}; use crate::zerror; use crate::{errors::ZResult, key_expr::process_kotlin_key_expr, throw_exception}; -use crate::{ - utils::{decode_byte_array, decode_encoding}, -}; use jni::{ objects::{JByteArray, JClass, JString}, sys::{jboolean, jint, jlong}, From 10b398ea81c2b95d6ceaa03cbb283908a078f7bc Mon Sep 17 00:00:00 2001 From: Darius Maitia Date: Thu, 19 Sep 2024 13:43:37 -0300 Subject: [PATCH 7/7] Fix rebase error --- zenoh-jni/src/session.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/zenoh-jni/src/session.rs b/zenoh-jni/src/session.rs index 6707952f1..8d29e0c41 100644 --- a/zenoh-jni/src/session.rs +++ b/zenoh-jni/src/session.rs @@ -1044,7 +1044,7 @@ pub unsafe extern "C" fn Java_io_zenoh_jni_JNISession_getPeersZidViaJNI( let ids = { let peers_zid = session.info().peers_zid().wait(); let ids = peers_zid.collect::>(); - ids_to_java_list(&mut env, ids).map_err(|err| jni_error!(err)) + ids_to_java_list(&mut env, ids).map_err(|err| zerror!(err)) } .unwrap_or_else(|err| { throw_exception!(env, err); @@ -1067,7 +1067,7 @@ pub unsafe extern "C" fn Java_io_zenoh_jni_JNISession_getRoutersZidViaJNI( let ids = { let peers_zid = session.info().routers_zid().wait(); let ids = peers_zid.collect::>(); - ids_to_java_list(&mut env, ids).map_err(|err| jni_error!(err)) + ids_to_java_list(&mut env, ids).map_err(|err| zerror!(err)) } .unwrap_or_else(|err| { throw_exception!(env, err); @@ -1090,7 +1090,7 @@ pub unsafe extern "C" fn Java_io_zenoh_jni_JNISession_getZidViaJNI( let zid = session.info().zid().wait(); env.byte_array_from_slice(&zid.to_le_bytes()) .map(|x| x.as_raw()) - .map_err(|err| jni_error!(err)) + .map_err(|err| zerror!(err)) } .unwrap_or_else(|err| { throw_exception!(env, err);