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 18, 2024
1 parent 8f94133 commit 731ce8a
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 4 deletions.
4 changes: 2 additions & 2 deletions ci/vendor-wit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ make_vendor "wasi" "
[email protected]
[email protected]
[email protected]
sockets@v0.2.1
sockets@2e9982e
"

make_vendor "wasi-http" "
Expand All @@ -51,7 +51,7 @@ make_vendor "wasi-http" "
[email protected]
[email protected]
[email protected]
sockets@v0.2.1
sockets@2e9982e
[email protected]
"

Expand Down
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 731ce8a

Please sign in to comment.