Skip to content

Commit

Permalink
Add function to downcast a stream error to a network-related error. S…
Browse files Browse the repository at this point in the history
…imilar to the existing filesystem-error-code and http-error-code functions. (#9276)
  • Loading branch information
badeend authored Oct 9, 2024
1 parent 75a10e1 commit cef7774
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
2 changes: 2 additions & 0 deletions crates/cli-flags/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,8 @@ wasmtime_option_group! {
pub tcp: Option<bool>,
/// Indicates whether `wasi:sockets` UDP support is enabled or not.
pub udp: Option<bool>,
/// Enable WASI APIs marked as: @unstable(feature = network-error-code)
pub network_error_code: Option<bool>,
/// Allows imports from the `wasi_unstable` core wasm module.
pub preview0: Option<bool>,
/// Inherit all environment variables from the parent process.
Expand Down
22 changes: 19 additions & 3 deletions crates/wasi/src/host/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ where

fn network_error_code(&mut self, err: Resource<Error>) -> anyhow::Result<Option<ErrorCode>> {
let err = self.table().get(&err)?;
let _ = err; // TODO: should fill in this implementation

if let Some(err) = err.downcast_ref::<std::io::Error>() {
return Ok(Some(ErrorCode::from(err)));
}

Ok(None)
}
}
Expand All @@ -39,8 +43,14 @@ where

impl From<io::Error> for ErrorCode {
fn from(value: io::Error) -> Self {
(&value).into()
}
}

impl From<&io::Error> for ErrorCode {
fn from(value: &io::Error) -> Self {
// Attempt the more detailed native error code first:
if let Some(errno) = Errno::from_io_error(&value) {
if let Some(errno) = Errno::from_io_error(value) {
return errno.into();
}

Expand Down Expand Up @@ -69,7 +79,13 @@ impl From<io::Error> for ErrorCode {

impl From<Errno> for ErrorCode {
fn from(value: Errno) -> Self {
match value {
(&value).into()
}
}

impl From<&Errno> for ErrorCode {
fn from(value: &Errno) -> Self {
match *value {
Errno::WOULDBLOCK => ErrorCode::WouldBlock,
#[allow(unreachable_patterns)] // EWOULDBLOCK and EAGAIN can have the same value.
Errno::AGAIN => ErrorCode::WouldBlock,
Expand Down
1 change: 1 addition & 0 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ impl RunCommon {
pub fn compute_wasi_features(&self) -> LinkOptions {
let mut options = LinkOptions::default();
options.cli_exit_with_code(self.common.wasi.cli_exit_with_code.unwrap_or(false));
options.network_error_code(self.common.wasi.network_error_code.unwrap_or(false));
options
}
}
Expand Down

0 comments on commit cef7774

Please sign in to comment.