Skip to content

Commit a00adba

Browse files
skletsundmitry-timofeev
authored andcommitted
Improve exceptions handling in service runtime [ECR-3077]. (#967)
Switched from `check_error_on_exception` to `panic_on_exception` as there is no need to distinguish between them.
1 parent 35f068d commit a00adba

3 files changed

Lines changed: 16 additions & 22 deletions

File tree

exonum-java-binding/core/rust/integration_tests/tests/service_runtime.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ fn load_nonexistent_artifact() {
7070
let artifact_path = "nonexistent_artifact.jar";
7171

7272
assert_panics(
73-
&format!("Unable to load artifact {}", artifact_path),
73+
&format!("Failed to load the service from {}", artifact_path),
7474
|| runtime.load_artifact(&artifact_path),
7575
);
7676
}
@@ -84,7 +84,7 @@ fn load_artifact_twice() {
8484
// The second loading attempt should fail.
8585
assert_panics(
8686
&format!(
87-
"Unable to load artifact {}",
87+
"Failed to load the service from {}",
8888
artifact_path.to_string_lossy()
8989
),
9090
|| runtime.load_artifact(&artifact_path),
@@ -96,9 +96,10 @@ fn load_failing_artifact() {
9696
let runtime = get_runtime();
9797
let artifact_path = create_service_artifact_non_loadable(runtime.get_executor());
9898

99-
assert_panics("Unable to load artifact", || {
100-
runtime.load_artifact(&artifact_path)
101-
});
99+
assert_panics(
100+
"Java exception: com.exonum.binding.core.runtime.ServiceLoadingException;",
101+
|| runtime.load_artifact(&artifact_path),
102+
);
102103
}
103104

104105
#[test]
@@ -108,7 +109,7 @@ fn non_instantiable_service() {
108109
let artifact_id = runtime.load_artifact(&artifact_path);
109110

110111
assert_panics(
111-
&format!("Unable to create service for artifact_id [{}]", artifact_id),
112+
"com.exonum.binding.fakes.services.invalidservice.NonInstantiableService",
112113
|| runtime.create_service(&artifact_id),
113114
);
114115
}
@@ -117,10 +118,9 @@ fn non_instantiable_service() {
117118
fn create_service_for_unknown_artifact() {
118119
let runtime = get_runtime();
119120

120-
assert_panics(
121-
"Unable to create service for artifact_id [unknown:artifact:id]",
122-
|| runtime.create_service("unknown:artifact:id"),
123-
);
121+
assert_panics("Unknown artifactId: unknown:artifact:id", || {
122+
runtime.create_service("unknown:artifact:id")
123+
});
124124
}
125125

126126
// Creates a new instance of JavaServiceRuntime for same JVM.

exonum-java-binding/core/rust/src/runtime/java_service_runtime.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use jni::{
2424
use proxy::ServiceProxy;
2525
use runtime::config::{self, Config, InternalConfig, JvmConfig, RuntimeConfig};
2626
use std::{path::Path, sync::Arc};
27-
use utils::{check_error_on_exception, convert_to_string, unwrap_jni};
27+
use utils::{convert_to_string, panic_on_exception, unwrap_jni};
2828
use Executor;
2929

3030
const SERVICE_RUNTIME_BOOTSTRAP_PATH: &str = "com/exonum/binding/app/ServiceRuntimeBootstrap";
@@ -84,7 +84,7 @@ impl JavaServiceRuntime {
8484
pub fn create_service(&self, artifact_id: &str) -> ServiceProxy {
8585
unwrap_jni(self.executor.with_attached(|env| {
8686
let artifact_id_obj: JObject = env.new_string(artifact_id)?.into();
87-
let service = check_error_on_exception(
87+
let service = panic_on_exception(
8888
env,
8989
env.call_method(
9090
self.service_runtime.as_obj(),
@@ -93,12 +93,6 @@ impl JavaServiceRuntime {
9393
&[artifact_id_obj.into()],
9494
),
9595
)
96-
.unwrap_or_else(|err_msg| {
97-
panic!(
98-
"Unable to create service for artifact_id [{}]: {}",
99-
artifact_id, err_msg
100-
)
101-
})
10296
.l()?;
10397
let service = env.new_global_ref(service)?;
10498
Ok(ServiceProxy::from_global_ref(
@@ -117,7 +111,7 @@ impl JavaServiceRuntime {
117111
unwrap_jni(self.executor.with_attached(|env| {
118112
let artifact_path = artifact_path.as_ref().to_str().unwrap();
119113
let artifact_path_obj: JObject = env.new_string(artifact_path)?.into();
120-
let artifact_id = check_error_on_exception(
114+
let artifact_id = panic_on_exception(
121115
env,
122116
env.call_method(
123117
self.service_runtime.as_obj(),
@@ -126,9 +120,6 @@ impl JavaServiceRuntime {
126120
&[artifact_path_obj.into()],
127121
),
128122
)
129-
.unwrap_or_else(|err_msg| {
130-
panic!("Unable to load artifact {}: {}", artifact_path, err_msg)
131-
})
132123
.l()?;
133124
convert_to_string(env, artifact_id)
134125
}))

exonum-java-binding/core/rust/src/utils/errors.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ pub fn panic_on_exception<T>(env: &JNIEnv, result: JniResult<T>) -> T {
4747
/// Java exceptions are converted into `Error`s with their descriptions, Java errors and JNI errors
4848
/// are treated as unrecoverable and result in a panic.
4949
///
50+
/// *This method shall be used only if it is needed to determine if a Java Exception
51+
/// is an instance of `java.lang.Error` or `java.lang.Exception`.*
52+
///
5053
/// Panics:
5154
/// - Panics if there is some JNI error.
5255
/// - If there is a pending Java exception that is a subclass of `java.lang.Error`.

0 commit comments

Comments
 (0)