From d8720cffe86c73f75364246356162c451a42655b Mon Sep 17 00:00:00 2001 From: Pat Hickey Date: Thu, 17 Dec 2020 18:12:15 -0800 Subject: [PATCH] unexpected io error: do our best based on e.kind() --- crates/wasi-c2/src/error.rs | 3 --- crates/wasi-c2/src/snapshots/preview_1.rs | 24 +++++++++++++++++++++-- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/crates/wasi-c2/src/error.rs b/crates/wasi-c2/src/error.rs index 47920ce6b2..cd896da1b5 100644 --- a/crates/wasi-c2/src/error.rs +++ b/crates/wasi-c2/src/error.rs @@ -34,9 +34,6 @@ pub enum Error { #[error("Unexpected IoError: {0}")] UnexpectedIo(#[source] std::io::Error), - #[error("Unsupported operation: {0}")] - Unsupported(String), - // Below this, all variants are from the `$errno` type: /// Errno::TooBig: Argument list too long #[error("TooBig: Argument list too long")] diff --git a/crates/wasi-c2/src/snapshots/preview_1.rs b/crates/wasi-c2/src/snapshots/preview_1.rs index 7757a7cf74..24eeb8f542 100644 --- a/crates/wasi-c2/src/snapshots/preview_1.rs +++ b/crates/wasi-c2/src/snapshots/preview_1.rs @@ -38,12 +38,33 @@ impl types::UserErrorConversion for WasiCtx { impl From for types::Errno { fn from(e: Error) -> types::Errno { + use std::io::ErrorKind; use types::Errno; match e { Error::Guest(e) => e.into(), Error::TryFromInt(_) => Errno::Overflow, Error::Utf8(_) => Errno::Ilseq, - Error::UnexpectedIo(_) => Errno::Io, + Error::UnexpectedIo(e) => match e.kind() { + ErrorKind::NotFound => Errno::Noent, + ErrorKind::PermissionDenied => Errno::Perm, + ErrorKind::AlreadyExists => Errno::Exist, + ErrorKind::InvalidInput => Errno::Ilseq, + ErrorKind::ConnectionRefused + | ErrorKind::ConnectionReset + | ErrorKind::ConnectionAborted + | ErrorKind::NotConnected + | ErrorKind::AddrInUse + | ErrorKind::AddrNotAvailable + | ErrorKind::BrokenPipe + | ErrorKind::WouldBlock + | ErrorKind::InvalidData + | ErrorKind::TimedOut + | ErrorKind::WriteZero + | ErrorKind::Interrupted + | ErrorKind::Other + | ErrorKind::UnexpectedEof + | _ => Errno::Io, + }, Error::GetRandom(_) => Errno::Io, Error::TooBig => Errno::TooBig, Error::Acces => Errno::Acces, @@ -75,7 +96,6 @@ impl From for types::Errno { Error::DirNotCapable { .. } => Errno::Notcapable, Error::NotCapable => Errno::Notcapable, Error::TableOverflow => Errno::Overflow, - Error::Unsupported { .. } => Errno::Notcapable, // XXX is this reasonable? } } }