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.
  • Loading branch information
badeend committed Sep 14, 2024
1 parent 8f94133 commit 7411bf1
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 2 deletions.
2 changes: 2 additions & 0 deletions crates/wasi-http/src/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod generated {
wasmtime::component::bindgen!({
path: "wit",
world: "wasi:http/proxy",
features: ["network-error-code"],
tracing: true,
// Flag this as "possibly async" which will cause the exports to be
// generated as async, but none of the imports here are async since
Expand Down Expand Up @@ -56,6 +57,7 @@ pub mod sync {
#![allow(missing_docs)]
wasmtime::component::bindgen!({
world: "wasi:http/proxy",
features: ["network-error-code"],
tracing: true,
async: false,
with: {
Expand Down
16 changes: 16 additions & 0 deletions crates/wasi-http/wit/deps/sockets/network.wit
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
@since(version = 0.2.0)
interface network {
@unstable(feature = network-error-code)
use wasi:io/error@0.2.1.{error};

/// An opaque resource that represents access to (a subset of) the network.
/// This enables context-based security for networking.
/// There is no need for this to map 1:1 to a physical network interface.
Expand Down Expand Up @@ -105,6 +108,19 @@ interface network {
permanent-resolver-failure,
}

/// Attempts to extract a network-related `error-code` from the stream
/// `error` provided.
///
/// Stream operations which return `stream-error::last-operation-failed`
/// have a payload with more information about the operation that failed.
/// This payload can be passed through to this function to see if there's
/// network-related information about the error to return.
///
/// Note that this function is fallible because not all stream-related
/// errors are network-related errors.
@unstable(feature = network-error-code)
network-error-code: func(err: borrow<error>) -> option<error-code>;

@since(version = 0.2.0)
enum ip-address-family {
/// Similar to `AF_INET` in POSIX.
Expand Down
2 changes: 2 additions & 0 deletions crates/wasi/src/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ pub mod sync {
wasmtime::component::bindgen!({
path: "wit",
world: "wasi:cli/command",
features: ["network-error-code"],
tracing: true,
trappable_error_type: {
"wasi:io/streams/stream-error" => StreamError,
Expand Down Expand Up @@ -326,6 +327,7 @@ mod async_io {
wasmtime::component::bindgen!({
path: "wit",
world: "wasi:cli/command",
features: ["network-error-code"],
tracing: true,
trappable_imports: true,
async: {
Expand Down
29 changes: 27 additions & 2 deletions crates/wasi/src/host/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@ where
fn convert_error_code(&mut self, error: SocketError) -> anyhow::Result<ErrorCode> {
error.downcast()
}

fn network_error_code(
&mut self,
err: Resource<anyhow::Error>,
) -> anyhow::Result<Option<ErrorCode>> {
let err = self.table().get(&err)?;

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

Ok(None)
}
}

impl<T> crate::bindings::sockets::network::HostNetwork for WasiImpl<T>
Expand All @@ -32,8 +45,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 @@ -62,7 +81,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
16 changes: 16 additions & 0 deletions crates/wasi/wit/deps/sockets/network.wit
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
@since(version = 0.2.0)
interface network {
@unstable(feature = network-error-code)
use wasi:io/error@0.2.1.{error};

/// An opaque resource that represents access to (a subset of) the network.
/// This enables context-based security for networking.
/// There is no need for this to map 1:1 to a physical network interface.
Expand Down Expand Up @@ -105,6 +108,19 @@ interface network {
permanent-resolver-failure,
}

/// Attempts to extract a network-related `error-code` from the stream
/// `error` provided.
///
/// Stream operations which return `stream-error::last-operation-failed`
/// have a payload with more information about the operation that failed.
/// This payload can be passed through to this function to see if there's
/// network-related information about the error to return.
///
/// Note that this function is fallible because not all stream-related
/// errors are network-related errors.
@unstable(feature = network-error-code)
network-error-code: func(err: borrow<error>) -> option<error-code>;

@since(version = 0.2.0)
enum ip-address-family {
/// Similar to `AF_INET` in POSIX.
Expand Down

0 comments on commit 7411bf1

Please sign in to comment.