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(ext/net): use concrete error type #26227

Merged
merged 12 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions ext/net/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pin-project.workspace = true
rustls-tokio-stream.workspace = true
serde.workspace = true
socket2.workspace = true
thiserror.workspace = true
tokio.workspace = true
trust-dns-proto = "0.23"
trust-dns-resolver = { version = "0.23", features = ["tokio-runtime", "serde-config"] }
41 changes: 25 additions & 16 deletions ext/net/io.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

use deno_core::error::generic_error;
use deno_core::error::AnyError;
use deno_core::futures::TryFutureExt;
use deno_core::AsyncMutFuture;
use deno_core::AsyncRefCell;
use deno_core::AsyncResult;
Expand Down Expand Up @@ -69,25 +68,36 @@ where
pub async fn read(
self: Rc<Self>,
data: &mut [u8],
) -> Result<usize, AnyError> {
) -> Result<usize, std::io::Error> {
let mut rd = self.rd_borrow_mut().await;
let nread = rd.read(data).try_or_cancel(self.cancel_handle()).await?;
Ok(nread)
}

pub async fn write(self: Rc<Self>, data: &[u8]) -> Result<usize, AnyError> {
pub async fn write(
self: Rc<Self>,
data: &[u8],
) -> Result<usize, std::io::Error> {
let mut wr = self.wr_borrow_mut().await;
let nwritten = wr.write(data).await?;
Ok(nwritten)
}

pub async fn shutdown(self: Rc<Self>) -> Result<(), AnyError> {
pub async fn shutdown(self: Rc<Self>) -> Result<(), std::io::Error> {
let mut wr = self.wr_borrow_mut().await;
wr.shutdown().await?;
Ok(())
}
}

#[derive(Debug, thiserror::Error)]
pub enum MapError {
#[error(transparent)]
Io(std::io::Error),
#[error("Unable to get resources")]
NoResources,
}

pub type TcpStreamResource =
FullDuplexResource<tcp::OwnedReadHalf, tcp::OwnedWriteHalf>;

Expand All @@ -100,7 +110,7 @@ impl Resource for TcpStreamResource {
}

fn shutdown(self: Rc<Self>) -> AsyncResult<()> {
Box::pin(self.shutdown())
Box::pin(self.shutdown().map_err(Into::into))
}

fn close(self: Rc<Self>) {
Expand All @@ -109,31 +119,30 @@ impl Resource for TcpStreamResource {
}

impl TcpStreamResource {
pub fn set_nodelay(self: Rc<Self>, nodelay: bool) -> Result<(), AnyError> {
self.map_socket(Box::new(move |socket| Ok(socket.set_nodelay(nodelay)?)))
pub fn set_nodelay(self: Rc<Self>, nodelay: bool) -> Result<(), MapError> {
self.map_socket(Box::new(move |socket| socket.set_nodelay(nodelay)))
}

pub fn set_keepalive(
self: Rc<Self>,
keepalive: bool,
) -> Result<(), AnyError> {
self
.map_socket(Box::new(move |socket| Ok(socket.set_keepalive(keepalive)?)))
) -> Result<(), MapError> {
self.map_socket(Box::new(move |socket| socket.set_keepalive(keepalive)))
}

#[allow(clippy::type_complexity)]
fn map_socket(
self: Rc<Self>,
map: Box<dyn FnOnce(SockRef) -> Result<(), AnyError>>,
) -> Result<(), AnyError> {
map: Box<dyn FnOnce(SockRef) -> Result<(), std::io::Error>>,
) -> Result<(), MapError> {
if let Some(wr) = RcRef::map(self, |r| &r.wr).try_borrow() {
let stream = wr.as_ref().as_ref();
let socket = socket2::SockRef::from(stream);

return map(socket);
return map(socket).map_err(MapError::Io);
}

Err(generic_error("Unable to get resources"))
Err(MapError::NoResources)
}
}

Expand Down Expand Up @@ -170,7 +179,7 @@ impl Resource for UnixStreamResource {
}

fn shutdown(self: Rc<Self>) -> AsyncResult<()> {
Box::pin(self.shutdown())
Box::pin(self.shutdown().map_err(Into::into))
}

fn close(self: Rc<Self>) {
Expand Down
Loading
Loading