Merge pull request #2140 from bytecodealliance/pch/wasi_error_handling

wasi-common: refactor error types
This commit is contained in:
Pat Hickey
2020-09-01 13:01:26 -07:00
committed by GitHub
34 changed files with 511 additions and 407 deletions

View File

@@ -1,15 +1,13 @@
//! Types and constants shared between 32-bit and 64-bit wasi. Types involving
//! pointer or `usize`-sized data are excluded here, so this file only contains
//! fixed-size types, so it's host/target independent.
use crate::WasiCtx;
use crate::{Error, Result, WasiCtx};
use tracing::debug;
wiggle::from_witx!({
witx: ["$WASI_ROOT/phases/snapshot/witx/wasi_snapshot_preview1.witx"],
ctx: WasiCtx,
errors: { errno => Error },
});
pub use types::Errno;
pub type Result<T> = std::result::Result<T, Errno>;
use types::Errno;
impl wiggle::GuestErrorType for Errno {
fn success() -> Self {
@@ -19,9 +17,54 @@ impl wiggle::GuestErrorType for Errno {
impl types::GuestErrorConversion for WasiCtx {
fn into_errno(&self, e: wiggle::GuestError) -> Errno {
eprintln!("Guest error: {:?}", e);
// TODO proper error mapping
Errno::Inval
debug!("Guest error: {:?}", e);
e.into()
}
}
impl types::UserErrorConversion for WasiCtx {
fn errno_from_error(&self, e: Error) -> Errno {
debug!("Error: {:?}", e);
e.into()
}
}
impl From<Error> for Errno {
fn from(e: Error) -> Errno {
match e {
Error::Guest(e) => e.into(),
Error::TryFromInt(_) => Errno::Overflow,
Error::Utf8(_) => Errno::Ilseq,
Error::UnexpectedIo(_) => Errno::Io,
Error::GetRandom(_) => Errno::Io,
Error::TooBig => Errno::TooBig,
Error::Acces => Errno::Acces,
Error::Badf => Errno::Badf,
Error::Busy => Errno::Busy,
Error::Exist => Errno::Exist,
Error::Fault => Errno::Fault,
Error::Fbig => Errno::Fbig,
Error::Ilseq => Errno::Ilseq,
Error::Inval => Errno::Inval,
Error::Io => Errno::Io,
Error::Isdir => Errno::Isdir,
Error::Loop => Errno::Loop,
Error::Mfile => Errno::Mfile,
Error::Mlink => Errno::Mlink,
Error::Nametoolong => Errno::Nametoolong,
Error::Nfile => Errno::Nfile,
Error::Noent => Errno::Noent,
Error::Nomem => Errno::Nomem,
Error::Nospc => Errno::Nospc,
Error::Notdir => Errno::Notdir,
Error::Notempty => Errno::Notempty,
Error::Notsup => Errno::Notsup,
Error::Overflow => Errno::Overflow,
Error::Pipe => Errno::Pipe,
Error::Perm => Errno::Perm,
Error::Spipe => Errno::Spipe,
Error::Notcapable => Errno::Notcapable,
}
}
}
@@ -45,24 +88,6 @@ impl From<wiggle::GuestError> for Errno {
}
}
impl From<std::convert::Infallible> for Errno {
fn from(_err: std::convert::Infallible) -> Self {
unreachable!()
}
}
impl From<std::num::TryFromIntError> for Errno {
fn from(_err: std::num::TryFromIntError) -> Self {
Self::Overflow
}
}
impl From<std::str::Utf8Error> for Errno {
fn from(_err: std::str::Utf8Error) -> Self {
Self::Ilseq
}
}
impl From<std::fs::FileType> for types::Filetype {
fn from(ftype: std::fs::FileType) -> Self {
if ftype.is_file() {