Merge pull request #2760 from bytecodealliance/pch/wiggle_error_reporting

wiggle: delete GuestErrorConversion, improve some error reporting
This commit is contained in:
Pat Hickey
2021-03-24 13:39:04 -07:00
committed by GitHub
23 changed files with 21 additions and 123 deletions

View File

@@ -24,14 +24,6 @@ impl wiggle::GuestErrorType for types::Errno {
} }
} }
impl types::GuestErrorConversion for WasiCtx {
fn into_errno(&self, e: wiggle::GuestError) -> types::Errno {
debug!("Guest error: {:?}", e);
let snapshot1_errno: snapshot1_types::Errno = e.into();
snapshot1_errno.into()
}
}
impl types::UserErrorConversion for WasiCtx { impl types::UserErrorConversion for WasiCtx {
fn errno_from_error(&self, e: Error) -> Result<types::Errno, wiggle::Trap> { fn errno_from_error(&self, e: Error) -> Result<types::Errno, wiggle::Trap> {
debug!("Error: {:?}", e); debug!("Error: {:?}", e);

View File

@@ -30,13 +30,6 @@ impl wiggle::GuestErrorType for types::Errno {
} }
} }
impl types::GuestErrorConversion for WasiCtx {
fn into_errno(&self, e: wiggle::GuestError) -> types::Errno {
debug!("Guest error: {:?}", e);
e.into()
}
}
impl types::UserErrorConversion for WasiCtx { impl types::UserErrorConversion for WasiCtx {
fn errno_from_error(&self, e: Error) -> Result<types::Errno, wiggle::Trap> { fn errno_from_error(&self, e: Error) -> Result<types::Errno, wiggle::Trap> {
debug!("Error: {:?}", e); debug!("Error: {:?}", e);
@@ -103,7 +96,6 @@ impl From<wiggle::GuestError> for types::Errno {
InvalidUtf8 { .. } => Self::Ilseq, InvalidUtf8 { .. } => Self::Ilseq,
TryFromIntError { .. } => Self::Overflow, TryFromIntError { .. } => Self::Overflow,
InFunc { err, .. } => types::Errno::from(*err), InFunc { err, .. } => types::Errno::from(*err),
InDataField { err, .. } => types::Errno::from(*err),
SliceLengthsDiffer { .. } => Self::Fault, SliceLengthsDiffer { .. } => Self::Fault,
BorrowCheckerOutOfHandles { .. } => Self::Fault, BorrowCheckerOutOfHandles { .. } => Self::Fault,
} }

View File

@@ -52,13 +52,6 @@ impl<'a> wiggle::GuestErrorType for guest_types::CryptoErrno {
} }
} }
impl guest_types::GuestErrorConversion for WasiCryptoCtx {
fn into_crypto_errno(&self, e: wiggle::GuestError) -> guest_types::CryptoErrno {
eprintln!("GuestError (witx) {:?}", e);
guest_types::CryptoErrno::GuestError
}
}
impl From<wiggle::GuestError> for guest_types::CryptoErrno { impl From<wiggle::GuestError> for guest_types::CryptoErrno {
fn from(e: wiggle::GuestError) -> Self { fn from(e: wiggle::GuestError) -> Self {
eprintln!("GuestError (impl) {:?}", e); eprintln!("GuestError (impl) {:?}", e);

View File

@@ -10,16 +10,6 @@ wiggle::from_witx!({
use types::NnErrno; use types::NnErrno;
/// Wiggle generates code that performs some input validation on the arguments passed in by users of
/// wasi-nn. Here we convert the validation error into one (or more, eventually) of the error
/// variants defined in the witx.
impl types::GuestErrorConversion for WasiNnCtx {
fn into_nn_errno(&self, e: wiggle::GuestError) -> NnErrno {
eprintln!("Guest error: {:?}", e);
NnErrno::InvalidArgument
}
}
impl<'a> types::UserErrorConversion for WasiNnCtx { impl<'a> types::UserErrorConversion for WasiNnCtx {
fn nn_errno_from_wasi_nn_error(&self, e: WasiNnError) -> Result<NnErrno, wiggle::Trap> { fn nn_errno_from_wasi_nn_error(&self, e: WasiNnError) -> Result<NnErrno, wiggle::Trap> {
eprintln!("Host error: {:?}", e); eprintln!("Host error: {:?}", e);

View File

@@ -139,10 +139,12 @@ impl witx::Bindgen for Rust<'_> {
) { ) {
let rt = self.rt; let rt = self.rt;
let wrap_err = |location: &str| { let wrap_err = |location: &str| {
let modulename = self.module.name.as_str();
let funcname = self.funcname; let funcname = self.funcname;
quote! { quote! {
|e| { |e| {
#rt::GuestError::InFunc { #rt::GuestError::InFunc {
modulename: #modulename,
funcname: #funcname, funcname: #funcname,
location: #location, location: #location,
err: Box::new(#rt::GuestError::from(e)), err: Box::new(#rt::GuestError::from(e)),

View File

@@ -38,17 +38,6 @@ pub fn generate(doc: &witx::Document, names: &Names, settings: &CodegenSettings)
} }
}); });
let guest_error_methods = doc.error_types().map(|t| {
let typename = names.type_ref(&t, anon_lifetime());
let err_method = names.guest_error_conversion_method(&t);
quote!(fn #err_method(&self, e: #rt::GuestError) -> #typename;)
});
let guest_error_conversion = quote! {
pub trait GuestErrorConversion {
#(#guest_error_methods)*
}
};
let user_error_methods = settings.errors.iter().map(|errtype| { let user_error_methods = settings.errors.iter().map(|errtype| {
let abi_typename = names.type_ref(&errtype.abi_type(), anon_lifetime()); let abi_typename = names.type_ref(&errtype.abi_type(), anon_lifetime());
let user_typename = errtype.typename(); let user_typename = errtype.typename();
@@ -82,7 +71,6 @@ pub fn generate(doc: &witx::Document, names: &Names, settings: &CodegenSettings)
#(#types)* #(#types)*
#(#constants)* #(#constants)*
#guest_error_conversion
#user_error_conversion #user_error_conversion
} }
#(#modules)* #(#modules)*

View File

@@ -196,11 +196,6 @@ impl Names {
} }
} }
pub fn guest_error_conversion_method(&self, tref: &TypeRef) -> Ident {
let suffix = Self::snake_typename(tref);
format_ident!("into_{}", suffix)
}
pub fn user_error_conversion_method(&self, user_type: &UserErrorType) -> Ident { pub fn user_error_conversion_method(&self, user_type: &UserErrorType) -> Ident {
let abi_type = Self::snake_typename(&user_type.abi_type()); let abi_type = Self::snake_typename(&user_type.abi_type());
format_ident!( format_ident!(

View File

@@ -113,17 +113,6 @@ use syn::parse_macro_input;
/// } /// }
/// } /// }
/// ///
/// /// The `types::GuestErrorConversion` trait is also generated with a method for
/// /// each type used in the `error` position. This trait allows wiggle-generated
/// /// code to convert a `wiggle::GuestError` into the right error type. The trait
/// /// must be implemented for the user's ctx type.
///
/// impl types::GuestErrorConversion for YourCtxType {
/// fn into_errno(&self, _e: wiggle::GuestError) -> types::Errno {
/// unimplemented!()
/// }
/// }
///
/// /// If you specify a `error` mapping to the macro, you must implement the /// /// If you specify a `error` mapping to the macro, you must implement the
/// /// `types::UserErrorConversion` for your ctx type as well. This trait gives /// /// `types::UserErrorConversion` for your ctx type as well. This trait gives
/// /// you an opportunity to store or log your rich error type, while returning /// /// you an opportunity to store or log your rich error type, while returning

View File

@@ -19,20 +19,14 @@ pub enum GuestError {
BorrowCheckerOutOfHandles, BorrowCheckerOutOfHandles,
#[error("Slice length mismatch")] #[error("Slice length mismatch")]
SliceLengthsDiffer, SliceLengthsDiffer,
#[error("In func {funcname}:{location}:")] #[error("In func {modulename}::{funcname} at {location}: {err}")]
InFunc { InFunc {
modulename: &'static str,
funcname: &'static str, funcname: &'static str,
location: &'static str, location: &'static str,
#[source] #[source]
err: Box<GuestError>, err: Box<GuestError>,
}, },
#[error("In data {typename}.{field}:")]
InDataField {
typename: String,
field: String,
#[source]
err: Box<GuestError>,
},
#[error("Invalid UTF-8 encountered: {0:?}")] #[error("Invalid UTF-8 encountered: {0:?}")]
InvalidUtf8(#[from] ::std::str::Utf8Error), InvalidUtf8(#[from] ::std::str::Utf8Error),
#[error("Int conversion error: {0:?}")] #[error("Int conversion error: {0:?}")]

View File

@@ -27,9 +27,7 @@ witx_literal: "
errors: { errno => RichError }, errors: { errno => RichError },
}); });
// The impl of GuestErrorConversion works just like in every other test where impl_errno!(types::Errno);
// we have a single error type with witx `$errno` with the success called `$ok`
impl_errno!(types::Errno, types::GuestErrorConversion);
/// When the `errors` mapping in witx is non-empty, we need to impl the /// When the `errors` mapping in witx is non-empty, we need to impl the
/// types::UserErrorConversion trait that wiggle generates from that mapping. /// types::UserErrorConversion trait that wiggle generates from that mapping.

View File

@@ -347,18 +347,11 @@ impl<'a> WasiCtx<'a> {
// with these errors. We just push them to vecs. // with these errors. We just push them to vecs.
#[macro_export] #[macro_export]
macro_rules! impl_errno { macro_rules! impl_errno {
( $errno:ty, $convert:path ) => { ( $errno:ty ) => {
impl wiggle::GuestErrorType for $errno { impl wiggle::GuestErrorType for $errno {
fn success() -> $errno { fn success() -> $errno {
<$errno>::Ok <$errno>::Ok
} }
} }
impl<'a> $convert for WasiCtx<'a> {
fn into_errno(&self, e: wiggle::GuestError) -> $errno {
eprintln!("GuestError: {:?}", e);
self.guest_errors.borrow_mut().push(e);
<$errno>::InvalidArg
}
}
}; };
} }

View File

@@ -6,7 +6,7 @@ wiggle::from_witx!({
witx: ["$CARGO_MANIFEST_DIR/tests/atoms.witx"], witx: ["$CARGO_MANIFEST_DIR/tests/atoms.witx"],
}); });
impl_errno!(types::Errno, types::GuestErrorConversion); impl_errno!(types::Errno);
impl<'a> atoms::Atoms for WasiCtx<'a> { impl<'a> atoms::Atoms for WasiCtx<'a> {
fn int_float_args(&self, an_int: u32, an_float: f32) -> Result<(), types::Errno> { fn int_float_args(&self, an_int: u32, an_float: f32) -> Result<(), types::Errno> {

View File

@@ -12,7 +12,7 @@ wiggle::from_witx!({
} }
}); });
impl_errno!(types::Errno, types::GuestErrorConversion); impl_errno!(types::Errno);
#[wiggle::async_trait(?Send)] #[wiggle::async_trait(?Send)]
impl<'a> atoms::Atoms for WasiCtx<'a> { impl<'a> atoms::Atoms for WasiCtx<'a> {

View File

@@ -26,9 +26,7 @@ mod convert_just_errno {
errors: { errno => RichError }, errors: { errno => RichError },
}); });
// The impl of GuestErrorConversion works just like in every other test where impl_errno!(types::Errno);
// we have a single error type with witx `$errno` with the success called `$ok`
impl_errno!(types::Errno, types::GuestErrorConversion);
/// When the `errors` mapping in witx is non-empty, we need to impl the /// When the `errors` mapping in witx is non-empty, we need to impl the
/// types::UserErrorConversion trait that wiggle generates from that mapping. /// types::UserErrorConversion trait that wiggle generates from that mapping.
@@ -104,7 +102,7 @@ mod convert_just_errno {
/// we use two distinct error types. /// we use two distinct error types.
mod convert_multiple_error_types { mod convert_multiple_error_types {
pub use super::convert_just_errno::RichError; pub use super::convert_just_errno::RichError;
use wiggle_test::WasiCtx; use wiggle_test::{impl_errno, WasiCtx};
/// Test that we can map multiple types of errors. /// Test that we can map multiple types of errors.
#[derive(Debug, thiserror::Error)] #[derive(Debug, thiserror::Error)]
@@ -135,27 +133,8 @@ mod convert_multiple_error_types {
errors: { errno => RichError, errno2 => AnotherRichError }, errors: { errno => RichError, errno2 => AnotherRichError },
}); });
// Can't use the impl_errno! macro as usual here because the conversion impl_errno!(types::Errno);
// trait ends up having two methods. impl_errno!(types::Errno2);
// We aren't going to execute this code, so the bodies are elided.
impl<'a> types::GuestErrorConversion for WasiCtx<'a> {
fn into_errno(&self, _e: wiggle::GuestError) -> types::Errno {
unimplemented!()
}
fn into_errno2(&self, _e: wiggle::GuestError) -> types::Errno2 {
unimplemented!()
}
}
impl wiggle::GuestErrorType for types::Errno {
fn success() -> types::Errno {
<types::Errno>::Ok
}
}
impl wiggle::GuestErrorType for types::Errno2 {
fn success() -> types::Errno2 {
<types::Errno2>::Ok
}
}
// The UserErrorConversion trait will also have two methods for this test. They correspond to // The UserErrorConversion trait will also have two methods for this test. They correspond to
// each member of the `errors` mapping. // each member of the `errors` mapping.

View File

@@ -7,7 +7,7 @@ wiggle::from_witx!({
witx: ["$CARGO_MANIFEST_DIR/tests/flags.witx"], witx: ["$CARGO_MANIFEST_DIR/tests/flags.witx"],
}); });
impl_errno!(types::Errno, types::GuestErrorConversion); impl_errno!(types::Errno);
impl<'a> flags::Flags for WasiCtx<'a> { impl<'a> flags::Flags for WasiCtx<'a> {
fn configure_car( fn configure_car(

View File

@@ -8,7 +8,7 @@ wiggle::from_witx!({
witx: ["$CARGO_MANIFEST_DIR/tests/handles.witx"], witx: ["$CARGO_MANIFEST_DIR/tests/handles.witx"],
}); });
impl_errno!(types::Errno, types::GuestErrorConversion); impl_errno!(types::Errno);
impl<'a> handle_examples::HandleExamples for WasiCtx<'a> { impl<'a> handle_examples::HandleExamples for WasiCtx<'a> {
fn fd_create(&self) -> Result<types::Fd, types::Errno> { fn fd_create(&self) -> Result<types::Fd, types::Errno> {

View File

@@ -7,7 +7,7 @@ wiggle::from_witx!({
witx: ["$CARGO_MANIFEST_DIR/tests/ints.witx"], witx: ["$CARGO_MANIFEST_DIR/tests/ints.witx"],
}); });
impl_errno!(types::Errno, types::GuestErrorConversion); impl_errno!(types::Errno);
impl<'a> ints::Ints for WasiCtx<'a> { impl<'a> ints::Ints for WasiCtx<'a> {
fn cookie_cutter(&self, init_cookie: types::Cookie) -> Result<types::Bool, types::Errno> { fn cookie_cutter(&self, init_cookie: types::Cookie) -> Result<types::Bool, types::Errno> {

View File

@@ -6,7 +6,7 @@ wiggle::from_witx!({
witx: ["$CARGO_MANIFEST_DIR/tests/lists.witx"], witx: ["$CARGO_MANIFEST_DIR/tests/lists.witx"],
}); });
impl_errno!(types::Errno, types::GuestErrorConversion); impl_errno!(types::Errno);
impl<'a> lists::Lists for WasiCtx<'a> { impl<'a> lists::Lists for WasiCtx<'a> {
fn reduce_excuses( fn reduce_excuses(

View File

@@ -6,7 +6,7 @@ wiggle::from_witx!({
witx: ["$CARGO_MANIFEST_DIR/tests/pointers.witx"], witx: ["$CARGO_MANIFEST_DIR/tests/pointers.witx"],
}); });
impl_errno!(types::Errno, types::GuestErrorConversion); impl_errno!(types::Errno);
impl<'a> pointers::Pointers for WasiCtx<'a> { impl<'a> pointers::Pointers for WasiCtx<'a> {
fn pointers_and_enums<'b>( fn pointers_and_enums<'b>(

View File

@@ -6,7 +6,7 @@ wiggle::from_witx!({
witx: ["$CARGO_MANIFEST_DIR/tests/records.witx"], witx: ["$CARGO_MANIFEST_DIR/tests/records.witx"],
}); });
impl_errno!(types::Errno, types::GuestErrorConversion); impl_errno!(types::Errno);
impl<'a> records::Records for WasiCtx<'a> { impl<'a> records::Records for WasiCtx<'a> {
fn sum_of_pair(&self, an_pair: &types::PairInts) -> Result<i64, types::Errno> { fn sum_of_pair(&self, an_pair: &types::PairInts) -> Result<i64, types::Errno> {

View File

@@ -6,7 +6,7 @@ wiggle::from_witx!({
witx: ["$CARGO_MANIFEST_DIR/tests/strings.witx"], witx: ["$CARGO_MANIFEST_DIR/tests/strings.witx"],
}); });
impl_errno!(types::Errno, types::GuestErrorConversion); impl_errno!(types::Errno);
impl<'a> strings::Strings for WasiCtx<'a> { impl<'a> strings::Strings for WasiCtx<'a> {
fn hello_string(&self, a_string: &GuestPtr<str>) -> Result<u32, types::Errno> { fn hello_string(&self, a_string: &GuestPtr<str>) -> Result<u32, types::Errno> {

View File

@@ -6,7 +6,7 @@ wiggle::from_witx!({
witx: ["$CARGO_MANIFEST_DIR/tests/variant.witx"], witx: ["$CARGO_MANIFEST_DIR/tests/variant.witx"],
}); });
impl_errno!(types::Errno, types::GuestErrorConversion); impl_errno!(types::Errno);
// Avoid panics on overflow // Avoid panics on overflow
fn mult_lose_overflow(a: i32, b: u32) -> i32 { fn mult_lose_overflow(a: i32, b: u32) -> i32 {

View File

@@ -1,4 +1,4 @@
use wiggle::{GuestError, GuestErrorType, GuestPtr, GuestSlice}; use wiggle::{GuestErrorType, GuestPtr, GuestSlice};
use wiggle_test::WasiCtx; use wiggle_test::WasiCtx;
// This test file exists to make sure that the entire `wasi.witx` file can be // This test file exists to make sure that the entire `wasi.witx` file can be
@@ -31,13 +31,6 @@ impl GuestErrorType for types::Errno {
} }
} }
impl<'a> types::GuestErrorConversion for WasiCtx<'a> {
fn into_errno(&self, e: GuestError) -> types::Errno {
eprintln!("GuestError {:?}", e);
types::Errno::Badf
}
}
impl<'a> crate::wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx<'a> { impl<'a> crate::wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx<'a> {
fn args_get(&self, _argv: &GuestPtr<GuestPtr<u8>>, _argv_buf: &GuestPtr<u8>) -> Result<()> { fn args_get(&self, _argv: &GuestPtr<GuestPtr<u8>>, _argv_buf: &GuestPtr<u8>) -> Result<()> {
unimplemented!("args_get") unimplemented!("args_get")