From d9cfc809a7bef1e0be994c0f1855d7272ffb1666 Mon Sep 17 00:00:00 2001 From: tison Date: Sun, 15 Oct 2023 23:40:56 +0800 Subject: [PATCH] refactor(bindings/java): explict error handling Signed-off-by: tison --- bindings/java/src/error.rs | 26 ++++---------------------- bindings/java/src/lib.rs | 4 +++- 2 files changed, 7 insertions(+), 23 deletions(-) diff --git a/bindings/java/src/error.rs b/bindings/java/src/error.rs index b22a85302c0c..7ee42e81e53a 100644 --- a/bindings/java/src/error.rs +++ b/bindings/java/src/error.rs @@ -29,12 +29,6 @@ pub(crate) struct Error { } impl Error { - pub(crate) fn unexpected(err: impl Into + Display) -> Error { - Error { - inner: opendal::Error::new(ErrorKind::Unexpected, &err.to_string()).set_source(err), - } - } - pub(crate) fn throw(&self, env: &mut JNIEnv) { if let Err(err) = self.do_throw(env) { match err { @@ -84,26 +78,14 @@ impl Error { } impl From for Error { - fn from(error: opendal::Error) -> Self { - Self { inner: error } + fn from(err: opendal::Error) -> Self { + Self { inner: err } } } impl From for Error { - fn from(error: jni::errors::Error) -> Self { - Error::unexpected(error) - } -} - -impl From for Error { - fn from(error: std::str::Utf8Error) -> Self { - Error::unexpected(error) - } -} - -impl From for Error { - fn from(error: std::string::FromUtf8Error) -> Self { - Error::unexpected(error) + fn from(err: jni::errors::Error) -> Self { + opendal::Error::new(ErrorKind::Unexpected, &err.to_string()).into() } } diff --git a/bindings/java/src/lib.rs b/bindings/java/src/lib.rs index 63adbb35b74a..97c6d0c5338d 100644 --- a/bindings/java/src/lib.rs +++ b/bindings/java/src/lib.rs @@ -135,7 +135,9 @@ fn make_presigned_request<'a>(env: &mut JNIEnv<'a>, req: PresignedRequest) -> Re let mut map = HashMap::new(); for (k, v) in req.header().iter() { let key = k.to_string(); - let value = v.to_str().map_err(Error::unexpected)?; + let value = v.to_str().map_err(|err| { + opendal::Error::new(opendal::ErrorKind::Unexpected, &err.to_string()) + })?; map.insert(key, value.to_owned()); } map