Tidy up the WASI ErrorKind enum. (#5015)

* Tidy up the WASI `ErrorKind` enum.

`ErrorKind` is an internal enum used in wasi-libc to represent WASI
errors that aren't precisely represened by `std::io::ErrorKind` errors.
Add a descriptive comment, and remove some codes that are no longer
needed:

 - Remove `NotCapable`, which is no longer used.
 - Remove `WouldBlk`, `Exist`, `Noent`, and `Inval`, which have
   one-to-one correspondences with codes in `std::io::ErrorKind`.

This will simplify the error handling in #4947 and #4967, as it means
the code will no longer have to check for two different forms of these
errors.

* Map `std::io::ErrorKind::InvalidInput` to `Ok(types::Errno::Inval)`.
This commit is contained in:
Dan Gohman
2022-10-05 07:29:49 -07:00
committed by GitHub
parent 6d1bce9c64
commit 24da5f7787
2 changed files with 13 additions and 26 deletions

View File

@@ -26,32 +26,27 @@
pub use anyhow::{Context, Error}; pub use anyhow::{Context, Error};
/// Internal error type for the `wasi-common` crate. /// 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)] #[derive(Copy, Clone, Debug, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive] #[non_exhaustive]
pub enum ErrorKind { 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 /// Errno::TooBig: Argument list too long
#[error("TooBig: Argument list too long")] #[error("TooBig: Argument list too long")]
TooBig, TooBig,
/// Errno::Badf: Bad file descriptor /// Errno::Badf: Bad file descriptor
#[error("Badf: Bad file descriptor")] #[error("Badf: Bad file descriptor")]
Badf, Badf,
/// Errno::Exist: File exists
#[error("Exist: File exists")]
Exist,
/// Errno::Ilseq: Illegal byte sequence /// Errno::Ilseq: Illegal byte sequence
#[error("Ilseq: Illegal byte sequence")] #[error("Ilseq: Illegal byte sequence")]
Ilseq, Ilseq,
/// Errno::Inval: Invalid argument
#[error("Inval: Invalid argument")]
Inval,
/// Errno::Io: I/O error /// Errno::Io: I/O error
#[error("Io: I/O error")] #[error("Io: I/O error")]
Io, Io,
@@ -76,9 +71,6 @@ pub enum ErrorKind {
/// Errno::Perm: Permission denied /// Errno::Perm: Permission denied
#[error("Permission denied")] #[error("Permission denied")]
Perm, Perm,
/// Errno::NotCapable: Not capable
#[error("Not capable")]
NotCapable,
} }
pub trait ErrorExt { pub trait ErrorExt {
@@ -104,7 +96,7 @@ impl ErrorExt for Error {
anyhow::anyhow!(msg.into()) anyhow::anyhow!(msg.into())
} }
fn not_found() -> Self { fn not_found() -> Self {
ErrorKind::Noent.into() std::io::Error::from(std::io::ErrorKind::NotFound).into()
} }
fn too_big() -> Self { fn too_big() -> Self {
ErrorKind::TooBig.into() ErrorKind::TooBig.into()
@@ -113,13 +105,13 @@ impl ErrorExt for Error {
ErrorKind::Badf.into() ErrorKind::Badf.into()
} }
fn exist() -> Self { fn exist() -> Self {
ErrorKind::Exist.into() std::io::Error::from(std::io::ErrorKind::AlreadyExists).into()
} }
fn illegal_byte_sequence() -> Self { fn illegal_byte_sequence() -> Self {
ErrorKind::Ilseq.into() ErrorKind::Ilseq.into()
} }
fn invalid_argument() -> Self { fn invalid_argument() -> Self {
ErrorKind::Inval.into() std::io::Error::from(std::io::ErrorKind::InvalidInput).into()
} }
fn io() -> Self { fn io() -> Self {
ErrorKind::Io.into() ErrorKind::Io.into()

View File

@@ -69,13 +69,9 @@ impl From<ErrorKind> for types::Errno {
fn from(e: ErrorKind) -> types::Errno { fn from(e: ErrorKind) -> types::Errno {
use types::Errno; use types::Errno;
match e { match e {
ErrorKind::WouldBlk => Errno::Again,
ErrorKind::Noent => Errno::Noent,
ErrorKind::TooBig => Errno::TooBig, ErrorKind::TooBig => Errno::TooBig,
ErrorKind::Badf => Errno::Badf, ErrorKind::Badf => Errno::Badf,
ErrorKind::Exist => Errno::Exist,
ErrorKind::Ilseq => Errno::Ilseq, ErrorKind::Ilseq => Errno::Ilseq,
ErrorKind::Inval => Errno::Inval,
ErrorKind::Io => Errno::Io, ErrorKind::Io => Errno::Io,
ErrorKind::Nametoolong => Errno::Nametoolong, ErrorKind::Nametoolong => Errno::Nametoolong,
ErrorKind::Notdir => Errno::Notdir, ErrorKind::Notdir => Errno::Notdir,
@@ -83,7 +79,6 @@ impl From<ErrorKind> for types::Errno {
ErrorKind::Overflow => Errno::Overflow, ErrorKind::Overflow => Errno::Overflow,
ErrorKind::Range => Errno::Range, ErrorKind::Range => Errno::Range,
ErrorKind::Spipe => Errno::Spipe, ErrorKind::Spipe => Errno::Spipe,
ErrorKind::NotCapable => Errno::Notcapable,
ErrorKind::Perm => Errno::Perm, ErrorKind::Perm => Errno::Perm,
} }
} }
@@ -261,7 +256,7 @@ impl TryFrom<std::io::Error> for types::Errno {
std::io::ErrorKind::NotFound => Ok(types::Errno::Noent), std::io::ErrorKind::NotFound => Ok(types::Errno::Noent),
std::io::ErrorKind::PermissionDenied => Ok(types::Errno::Perm), std::io::ErrorKind::PermissionDenied => Ok(types::Errno::Perm),
std::io::ErrorKind::AlreadyExists => Ok(types::Errno::Exist), 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"))), _ => Err(anyhow::anyhow!(err).context(format!("Unknown OS error"))),
}, },
} }