holochain_wasmer_host/
error.rs

1use holochain_wasmer_common::WasmError;
2use serde::{Deserialize, Serialize};
3use thiserror::Error;
4
5/// Wraps a WasmErrorInner with a file and line number.
6/// The easiest way to generate this is with the `wasm_error!` macro that will
7/// insert the correct file/line and can create strings by forwarding args to
8/// the `format!` macro.
9#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize, Error)]
10#[rustfmt::skip]
11pub struct WasmHostError(pub WasmError);
12
13impl std::fmt::Display for WasmHostError {
14    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
15        write!(f, "{:?}", self.0)
16    }
17}
18
19impl From<WasmError> for WasmHostError {
20    fn from(wasm_error: WasmError) -> WasmHostError {
21        WasmHostError(wasm_error)
22    }
23}
24
25impl From<WasmHostError> for wasmer::RuntimeError {
26    fn from(wasm_error: WasmHostError) -> wasmer::RuntimeError {
27        wasmer::RuntimeError::user(Box::new(wasm_error.0))
28    }
29}
30
31#[macro_export]
32macro_rules! wasm_host_error {
33    ($e:expr) => {
34      WasmHostError(WasmError {
35          // On Windows the `file!()` macro returns a path with inconsistent formatting:
36          // from the workspace to the package root it uses backwards-slashes,
37          // then within the package it uses forwards-slashes.
38          // i.e. "test-crates\\wasm_core\\src/wasm.rs"
39          //
40          // To remedy this we normalize the formatting here.
41          file: file!().replace('\\', "/").to_string(),
42          line: line!(),
43          error: $e.into(),
44      })
45    };
46    ($($arg:tt)*) => {{
47        $crate::wasm_host_error!(std::format!($($arg)*))
48    }};
49}