Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(bindings/java): explict error handling #3288

Merged
merged 1 commit into from
Oct 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())
Xuanwo marked this conversation as resolved.
Show resolved Hide resolved
})?;
map.insert(key, value.to_owned());
}
map
Expand Down
Loading