Skip to content

Commit

Permalink
tidy some code
Browse files Browse the repository at this point in the history
Signed-off-by: tison <[email protected]>
  • Loading branch information
tisonkun committed Oct 8, 2024
1 parent 1affc36 commit 3d7cf85
Showing 1 changed file with 16 additions and 24 deletions.
40 changes: 16 additions & 24 deletions bindings/nodejs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -740,12 +740,7 @@ impl BlockingReader {
#[napi]
pub fn read(&mut self, mut buf: Buffer) -> Result<usize> {
let buf = buf.as_mut();
let n = self.inner.read(buf).map_err(|err| {
format_napi_error(
opendal::Error::new(opendal::ErrorKind::Unexpected, err.to_string())
.set_source(err),
)
})?;
let n = self.inner.read(buf).map_err(format_io_error)?;
Ok(n)
}
}
Expand All @@ -767,24 +762,7 @@ impl Reader {
#[napi]
pub async unsafe fn read(&mut self, mut buf: Buffer) -> Result<usize> {
let buf = buf.as_mut();
let n = self
.inner
.read(buf)
.await
.map_err(|e| {
opendal::Error::new(
match e.kind() {
std::io::ErrorKind::NotFound => opendal::ErrorKind::NotFound,
std::io::ErrorKind::PermissionDenied => {
opendal::ErrorKind::PermissionDenied
}
_ => opendal::ErrorKind::Unexpected,
},
"failed to read bytes",
)
.set_source(e)
})
.map_err(format_napi_error)?;
let n = self.inner.read(buf).await.map_err(format_io_error)?;
Ok(n)
}
}
Expand Down Expand Up @@ -1204,6 +1182,20 @@ impl AsRef<str> for ErrorCode {
}
}

fn format_io_error(e: std::io::Error) -> Error<ErrorCode> {
format_napi_error(
opendal::Error::new(
match e.kind() {
std::io::ErrorKind::NotFound => opendal::ErrorKind::NotFound,
std::io::ErrorKind::PermissionDenied => opendal::ErrorKind::PermissionDenied,
_ => opendal::ErrorKind::Unexpected,
},
"failed to read bytes",
)
.set_source(e),
)
}

/// Format opendal error to napi error.
fn format_napi_error(err: opendal::Error) -> Error<ErrorCode> {
Error::new(
Expand Down

0 comments on commit 3d7cf85

Please sign in to comment.