Skip to content

Commit 7671a22

Browse files
committed
exception wrapper for api namespace
1 parent 9eb2498 commit 7671a22

27 files changed

Lines changed: 199 additions & 155 deletions

zenoh-java/src/commonMain/kotlin/io/zenoh/Config.kt

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414

1515
package io.zenoh
1616

17-
import io.zenoh.jni.ZError
17+
import io.zenoh.exceptions.ZError
18+
import io.zenoh.exceptions.jniCall
1819
import io.zenoh.jni.JNIConfig
1920
import java.io.File
2021
import java.nio.file.Path
@@ -58,9 +59,7 @@ class Config internal constructor(internal val jniConfig: JNIConfig) {
5859
*/
5960
@JvmStatic
6061
@Throws(ZError::class)
61-
fun fromFile(file: File): Config {
62-
return Config(JNIConfig.loadFromFile(file.toString()))
63-
}
62+
fun fromFile(file: File): Config = jniCall { Config(JNIConfig.loadFromFile(file.toString())) }
6463

6564
/**
6665
* Loads the configuration from the [Path] specified.
@@ -71,9 +70,7 @@ class Config internal constructor(internal val jniConfig: JNIConfig) {
7170
*/
7271
@JvmStatic
7372
@Throws(ZError::class)
74-
fun fromFile(path: Path): Config {
75-
return Config(JNIConfig.loadFromFile(path.toString()))
76-
}
73+
fun fromFile(path: Path): Config = jniCall { Config(JNIConfig.loadFromFile(path.toString())) }
7774

7875
/**
7976
* Loads the configuration from json-formatted string.
@@ -86,9 +83,7 @@ class Config internal constructor(internal val jniConfig: JNIConfig) {
8683
*/
8784
@JvmStatic
8885
@Throws(ZError::class)
89-
fun fromJson(config: String): Config {
90-
return Config(JNIConfig.loadFromJson(config))
91-
}
86+
fun fromJson(config: String): Config = jniCall { Config(JNIConfig.loadFromJson(config)) }
9287

9388
/**
9489
* Loads the configuration from json5-formatted string.
@@ -101,9 +96,7 @@ class Config internal constructor(internal val jniConfig: JNIConfig) {
10196
*/
10297
@JvmStatic
10398
@Throws(ZError::class)
104-
fun fromJson5(config: String): Config {
105-
return Config(JNIConfig.loadFromJson(config))
106-
}
99+
fun fromJson5(config: String): Config = jniCall { Config(JNIConfig.loadFromJson(config)) }
107100

108101
/**
109102
* Loads the configuration from yaml-formatted string.
@@ -116,9 +109,7 @@ class Config internal constructor(internal val jniConfig: JNIConfig) {
116109
*/
117110
@JvmStatic
118111
@Throws(ZError::class)
119-
fun fromYaml(config: String): Config {
120-
return Config(JNIConfig.loadFromYaml(config))
121-
}
112+
fun fromYaml(config: String): Config = jniCall { Config(JNIConfig.loadFromYaml(config)) }
122113

123114
/**
124115
* Loads the configuration from the env variable [CONFIG_ENV].
@@ -127,12 +118,12 @@ class Config internal constructor(internal val jniConfig: JNIConfig) {
127118
*/
128119
@JvmStatic
129120
@Throws(ZError::class)
130-
fun fromEnv(): Config {
121+
fun fromEnv(): Config = jniCall {
131122
val envValue = System.getenv(CONFIG_ENV)
132123
if (envValue != null) {
133-
return fromFile(File(envValue))
124+
fromFile(File(envValue))
134125
} else {
135-
throw Exception("Couldn't load env variable: $CONFIG_ENV.")
126+
throw ZError("Couldn't load env variable: $CONFIG_ENV.")
136127
}
137128
}
138129
}
@@ -141,17 +132,13 @@ class Config internal constructor(internal val jniConfig: JNIConfig) {
141132
* The json value associated to the [key].
142133
*/
143134
@Throws(ZError::class)
144-
fun getJson(key: String): String {
145-
return jniConfig.getJson(key)
146-
}
135+
fun getJson(key: String): String = jniCall { jniConfig.getJson(key) }
147136

148137
/**
149138
* Inserts a json5 value associated to the [key] into the Config.
150139
*/
151140
@Throws(ZError::class)
152-
fun insertJson5(key: String, value: String) {
153-
jniConfig.insertJson5(key, value)
154-
}
141+
fun insertJson5(key: String, value: String) = jniCall { jniConfig.insertJson5(key, value) }
155142

156143
protected fun finalize() {
157144
jniConfig.free()

zenoh-java/src/commonMain/kotlin/io/zenoh/Logger.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414

1515
package io.zenoh
1616

17-
import io.zenoh.jni.ZError
17+
import io.zenoh.exceptions.ZError
18+
import io.zenoh.exceptions.jniCall
1819
import io.zenoh.jni.JNILogger
1920

2021
/** Logger class to redirect the Rust logs from Zenoh to the kotlin environment. */
@@ -31,6 +32,6 @@ internal class Logger {
3132
* See https://docs.rs/env_logger/latest/env_logger/index.html for accepted filter format.
3233
*/
3334
@Throws(ZError::class)
34-
fun start(filter: String) = JNILogger.startLogs(filter)
35+
fun start(filter: String) = jniCall { JNILogger.startLogs(filter) }
3536
}
3637
}

0 commit comments

Comments
 (0)