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

Tidy up the WASI ErrorKind enum. #5015

Merged
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
32 changes: 12 additions & 20 deletions crates/wasi-common/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,27 @@
pub use anyhow::{Context, Error};

/// Internal error type for the `wasi-common` crate.
/// Contains variants of the WASI `$errno` type are added according to what is actually used internally by
/// the crate. Not all values are represented presently.
///
/// This Contains variants of the WASI `$errno` type that are used internally
/// by the crate, and which aren't one-to-one with a `std::io::ErrorKind`
/// error.
///
/// When the Rust [io_error_more] feature is stabilized, that will enable
/// us to replace several more of these codes with `std::io::ErrorKind` codes.
///
/// [io_error_more]: https://doc.rust-lang.org/beta/unstable-book/library-features/io-error-more.html
#[derive(Copy, Clone, Debug, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum ErrorKind {
/// Errno::WouldBlk: Would block
#[error("WouldBlk: Would block")]
WouldBlk,
/// Errno::Noent: No such file or directory
#[error("Noent: No such file or directory")]
Noent,
/// Errno::TooBig: Argument list too long
#[error("TooBig: Argument list too long")]
TooBig,
/// Errno::Badf: Bad file descriptor
#[error("Badf: Bad file descriptor")]
Badf,
/// Errno::Exist: File exists
#[error("Exist: File exists")]
Exist,
/// Errno::Ilseq: Illegal byte sequence
#[error("Ilseq: Illegal byte sequence")]
Ilseq,
/// Errno::Inval: Invalid argument
#[error("Inval: Invalid argument")]
Inval,
/// Errno::Io: I/O error
#[error("Io: I/O error")]
Io,
Expand All @@ -76,9 +71,6 @@ pub enum ErrorKind {
/// Errno::Perm: Permission denied
#[error("Permission denied")]
Perm,
/// Errno::NotCapable: Not capable
#[error("Not capable")]
NotCapable,
}

pub trait ErrorExt {
Expand All @@ -104,7 +96,7 @@ impl ErrorExt for Error {
anyhow::anyhow!(msg.into())
}
fn not_found() -> Self {
ErrorKind::Noent.into()
std::io::Error::from(std::io::ErrorKind::NotFound).into()
}
fn too_big() -> Self {
ErrorKind::TooBig.into()
Expand All @@ -113,13 +105,13 @@ impl ErrorExt for Error {
ErrorKind::Badf.into()
}
fn exist() -> Self {
ErrorKind::Exist.into()
std::io::Error::from(std::io::ErrorKind::AlreadyExists).into()
}
fn illegal_byte_sequence() -> Self {
ErrorKind::Ilseq.into()
}
fn invalid_argument() -> Self {
ErrorKind::Inval.into()
std::io::Error::from(std::io::ErrorKind::InvalidInput).into()
}
fn io() -> Self {
ErrorKind::Io.into()
Expand Down
7 changes: 1 addition & 6 deletions crates/wasi-common/src/snapshots/preview_1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,16 @@ impl From<ErrorKind> for types::Errno {
fn from(e: ErrorKind) -> types::Errno {
use types::Errno;
match e {
ErrorKind::WouldBlk => Errno::Again,
ErrorKind::Noent => Errno::Noent,
ErrorKind::TooBig => Errno::TooBig,
ErrorKind::Badf => Errno::Badf,
ErrorKind::Exist => Errno::Exist,
ErrorKind::Ilseq => Errno::Ilseq,
ErrorKind::Inval => Errno::Inval,
ErrorKind::Io => Errno::Io,
ErrorKind::Nametoolong => Errno::Nametoolong,
ErrorKind::Notdir => Errno::Notdir,
ErrorKind::Notsup => Errno::Notsup,
ErrorKind::Overflow => Errno::Overflow,
ErrorKind::Range => Errno::Range,
ErrorKind::Spipe => Errno::Spipe,
ErrorKind::NotCapable => Errno::Notcapable,
ErrorKind::Perm => Errno::Perm,
}
}
Expand Down Expand Up @@ -261,7 +256,7 @@ impl TryFrom<std::io::Error> for types::Errno {
std::io::ErrorKind::NotFound => Ok(types::Errno::Noent),
std::io::ErrorKind::PermissionDenied => Ok(types::Errno::Perm),
std::io::ErrorKind::AlreadyExists => Ok(types::Errno::Exist),
std::io::ErrorKind::InvalidInput => Ok(types::Errno::Ilseq),
std::io::ErrorKind::InvalidInput => Ok(types::Errno::Inval),
_ => Err(anyhow::anyhow!(err).context(format!("Unknown OS error"))),
},
}
Expand Down