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 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
13 changes: 7 additions & 6 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ repository = "https://github.com/denoland/deno"

[workspace.dependencies]
deno_ast = { version = "=0.42.2", features = ["transpiling"] }
deno_core = { version = "0.313.0" }
deno_core = { version = "0.314.1" }

deno_bench_util = { version = "0.165.0", path = "./bench_util" }
deno_lockfile = "=0.23.1"
Expand Down
2 changes: 1 addition & 1 deletion ext/cache/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub enum CacheError {
Resource(deno_core::error::AnyError),
#[error(transparent)]
Other(deno_core::error::AnyError),
#[error(transparent)]
#[error("{0}")]
Io(#[from] std::io::Error),
}

Expand Down
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"] }
45 changes: 28 additions & 17 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("{0}")]
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 All @@ -153,7 +162,9 @@ impl UnixStreamResource {
unreachable!()
}
#[allow(clippy::unused_async)]
pub async fn shutdown(self: Rc<Self>) -> Result<(), AnyError> {
pub async fn shutdown(
self: Rc<Self>,
) -> Result<(), deno_core::error::AnyError> {
unreachable!()
}
pub fn cancel_read_ops(&self) {
Expand All @@ -170,7 +181,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