Skip to content

Commit

Permalink
refactor(bindings/java): explict error handling (#3288)
Browse files Browse the repository at this point in the history
Signed-off-by: tison <[email protected]>
  • Loading branch information
tisonkun authored Oct 15, 2023
1 parent 8d9c7f4 commit 778573f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 23 deletions.
26 changes: 4 additions & 22 deletions bindings/java/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@ pub(crate) struct Error {
}

impl Error {
pub(crate) fn unexpected(err: impl Into<anyhow::Error> + 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 {
Expand Down Expand Up @@ -84,26 +78,14 @@ impl Error {
}

impl From<opendal::Error> for Error {
fn from(error: opendal::Error) -> Self {
Self { inner: error }
fn from(err: opendal::Error) -> Self {
Self { inner: err }
}
}

impl From<jni::errors::Error> for Error {
fn from(error: jni::errors::Error) -> Self {
Error::unexpected(error)
}
}

impl From<std::str::Utf8Error> for Error {
fn from(error: std::str::Utf8Error) -> Self {
Error::unexpected(error)
}
}

impl From<std::string::FromUtf8Error> 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()
}
}

Expand Down
4 changes: 3 additions & 1 deletion bindings/java/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 778573f

Please sign in to comment.