#[derive(Debug, thiserror::Error)]
pub enum WasmGuardError {
#[error("failed to read WASM module at {path}: {reason}")]
ModuleLoad { path: String, reason: String },
#[error("WASM module compilation failed: {0}")]
Compilation(String),
#[error("missing required export: {0}")]
MissingExport(String),
#[error("invalid export signature for {name}: {reason}")]
InvalidSignature { name: String, reason: String },
#[error("fuel exhausted after {consumed} units (limit: {limit})")]
FuelExhausted { consumed: u64, limit: u64 },
#[error("guest memory error: {0}")]
Memory(String),
#[error("serialization error: {0}")]
Serialization(String),
#[error("WASM trap: {0}")]
Trap(String),
#[error("host function error: {0}")]
HostFunction(String),
#[error("module imports from forbidden namespace \"{module}\": import \"{name}\"")]
ImportViolation { module: String, name: String },
#[error("module size {size} bytes exceeds limit of {limit} bytes")]
ModuleTooLarge { size: usize, limit: usize },
#[error("WASM runtime backend not available -- enable the 'wasmtime-runtime' feature")]
BackendUnavailable,
#[error("failed to parse guard manifest: {0}")]
ManifestParse(String),
#[error("failed to load guard manifest for {path}: {reason}")]
ManifestLoad { path: String, reason: String },
#[error("wasm hash mismatch: expected {expected}, got {actual}")]
HashMismatch { expected: String, actual: String },
#[error("unsupported abi_version \"{version}\" (supported: {supported})")]
UnsupportedAbiVersion { version: String, supported: String },
#[error("unsupported wit_world {declared}; required \"{required}\". See {migration}")]
UnsupportedWitWorld {
declared: String,
required: String,
migration: &'static str,
},
#[error("unrecognized WASM format: neither core module nor component")]
UnrecognizedFormat,
#[error("signature verification failed: {0}")]
SignatureVerification(String),
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn error_import_violation_display() {
let err = WasmGuardError::ImportViolation {
module: "wasi".to_string(),
name: "fd_write".to_string(),
};
assert_eq!(
err.to_string(),
"module imports from forbidden namespace \"wasi\": import \"fd_write\""
);
}
#[test]
fn error_module_too_large_display() {
let err = WasmGuardError::ModuleTooLarge {
size: 20_000_000,
limit: 10_485_760,
};
assert_eq!(
err.to_string(),
"module size 20000000 bytes exceeds limit of 10485760 bytes"
);
}
}