better error trait design

This commit is contained in:
Pat Hickey
2020-01-23 11:21:04 -08:00
parent 7cc0073a3e
commit cb24fd97c0
5 changed files with 29 additions and 72 deletions

View File

@@ -1,18 +1,30 @@
pub mod test {
// FIXME: parameterize macro on what ctx type is used here
generate::from_witx!("test.witx");
pub struct WasiCtx {} // FIXME: parameterize macro on what ctx type is used here
pub struct WasiCtx {
mem_errors: Vec<::memory::MemoryError>,
value_errors: Vec<::memory::GuestValueError>,
}
impl types::WitxErrorConversion for WasiCtx {
fn success_to_errno(&mut self) -> types::Errno {
types::Errno::Ok
// Errno is used as a first return value in the functions above, therefore
// it must implement GuestError with type Context = WasiCtx.
// The context type should let you do logging or debugging or whatever you need
// with these errors. We just push them to vecs.
impl ::memory::GuestError for types::Errno {
type Context = WasiCtx;
fn is_success(&self) -> bool {
match self {
types::Errno::Ok => true,
_ => false,
}
}
fn memory_error_to_errno(&mut self, e: ::memory::MemoryError) -> types::Errno {
eprintln!("memory error: {:?}", e);
fn from_memory_error(e: ::memory::MemoryError, ctx: &mut WasiCtx) -> types::Errno {
ctx.mem_errors.push(e);
types::Errno::InvalidArg
}
fn value_error_to_errno(&mut self, e: ::memory::GuestValueError) -> types::Errno {
eprintln!("guest value error: {:?}", e);
fn from_value_error(e: ::memory::GuestValueError, ctx: &mut WasiCtx) -> types::Errno {
ctx.value_errors.push(e);
types::Errno::InvalidArg
}
}