wasmtime_types/
error.rs

1use thiserror::Error;
2
3/// A WebAssembly translation error.
4///
5/// When a WebAssembly function can't be translated, one of these error codes will be returned
6/// to describe the failure.
7#[derive(Error, Debug)]
8pub enum WasmError {
9    /// The input WebAssembly code is invalid.
10    ///
11    /// This error code is used by a WebAssembly translator when it encounters invalid WebAssembly
12    /// code. This should never happen for validated WebAssembly code.
13    #[error("Invalid input WebAssembly code at offset {offset}: {message}")]
14    InvalidWebAssembly {
15        /// A string describing the validation error.
16        message: String,
17        /// The bytecode offset where the error occurred.
18        offset: usize,
19    },
20
21    /// A feature used by the WebAssembly code is not supported by the embedding environment.
22    ///
23    /// Embedding environments may have their own limitations and feature restrictions.
24    #[error("Unsupported feature: {0}")]
25    Unsupported(String),
26
27    /// An implementation limit was exceeded.
28    ///
29    /// Cranelift can compile very large and complicated functions, but the [implementation has
30    /// limits][limits] that cause compilation to fail when they are exceeded.
31    ///
32    /// [limits]: https://github.com/bytecodealliance/wasmtime/blob/main/cranelift/docs/ir.md#implementation-limits
33    #[error("Implementation limit exceeded")]
34    ImplLimitExceeded,
35
36    /// Any user-defined error.
37    #[error("User error: {0}")]
38    User(String),
39}
40
41/// Return an `Err(WasmError::Unsupported(msg))` where `msg` the string built by calling `format!`
42/// on the arguments to this macro.
43#[macro_export]
44macro_rules! wasm_unsupported {
45    ($($arg:tt)*) => { $crate::WasmError::Unsupported(format!($($arg)*)) }
46}
47
48impl From<wasmparser::BinaryReaderError> for WasmError {
49    /// Convert from a `BinaryReaderError` to a `WasmError`.
50    fn from(e: wasmparser::BinaryReaderError) -> Self {
51        Self::InvalidWebAssembly {
52            message: e.message().into(),
53            offset: e.offset(),
54        }
55    }
56}
57
58/// A convenient alias for a `Result` that uses `WasmError` as the error type.
59pub type WasmResult<T> = Result<T, WasmError>;