Skip to content

Commit

Permalink
wasix: Improve SpawnError::Other
Browse files Browse the repository at this point in the history
Use Box<dyn Error> instead of a string.

Also adds an AnyhowStdError wrapper to allow converting an anyhow::Error to a
Box<dyn Error>.

Needed for better error handling/propagation.
  • Loading branch information
theduke committed Jun 21, 2023
1 parent 16124c0 commit b53917d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
29 changes: 28 additions & 1 deletion lib/wasix/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ pub enum SpawnError {
#[error("runtime error")]
Runtime(#[from] WasiRuntimeError),
#[error("{0}")]
Other(String),
Other(#[from] Box<dyn std::error::Error + Send + Sync>),
}

impl SpawnError {
Expand Down Expand Up @@ -222,6 +222,33 @@ impl WasiRuntimeError {
}
}

/// Wrapper for [`anyhow::Error`] that implements [`std::error::Error`].
pub struct AnyhowStdError(pub anyhow::Error);

impl std::fmt::Debug for AnyhowStdError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}

impl std::fmt::Display for AnyhowStdError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}

impl std::error::Error for AnyhowStdError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
Some(&*self.0)
}
}

impl From<anyhow::Error> for AnyhowStdError {
fn from(error: anyhow::Error) -> Self {
Self(error)
}
}

#[allow(clippy::result_large_err)]
pub(crate) fn run_wasi_func(
func: &wasmer::Function,
Expand Down
2 changes: 1 addition & 1 deletion lib/wasix/src/os/console/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ impl Console {
.with_capabilities(self.capabilities.clone())
.prepare_webc_env(prog, &wasi_opts, &pkg, self.runtime.clone(), Some(root_fs))
// TODO: better error conversion
.map_err(|err| SpawnError::Other(err.to_string()))?;
.map_err(|err| SpawnError::Other(Box::new(crate::AnyhowStdError(err))))?;

// TODO: no unwrap!
let env = builder
Expand Down

0 comments on commit b53917d

Please sign in to comment.