Skip to main content

rns_hooks/
error.rs

1use std::fmt;
2
3/// Errors that can occur in the hook system.
4#[derive(Debug)]
5pub enum HookError {
6    /// WASM module failed to compile.
7    CompileError(String),
8    /// WASM module failed to instantiate.
9    InstantiationError(String),
10    /// WASM execution ran out of fuel.
11    FuelExhausted,
12    /// WASM execution trapped (panic, out-of-bounds, etc.).
13    Trap(String),
14    /// Hook returned invalid result data.
15    InvalidResult(String),
16    /// Hook was auto-disabled after too many consecutive failures.
17    AutoDisabled { name: String, consecutive_traps: u32 },
18    /// Hook point not found or invalid.
19    InvalidHookPoint(String),
20    /// I/O error loading a WASM module.
21    IoError(std::io::Error),
22    /// WASM module was compiled against an incompatible ABI version.
23    AbiVersionMismatch {
24        hook_name: String,
25        expected: i32,
26        found: Option<i32>,
27    },
28}
29
30impl fmt::Display for HookError {
31    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32        match self {
33            HookError::CompileError(msg) => write!(f, "compile error: {}", msg),
34            HookError::InstantiationError(msg) => write!(f, "instantiation error: {}", msg),
35            HookError::FuelExhausted => write!(f, "fuel exhausted"),
36            HookError::Trap(msg) => write!(f, "trap: {}", msg),
37            HookError::InvalidResult(msg) => write!(f, "invalid result: {}", msg),
38            HookError::AutoDisabled { name, consecutive_traps } => {
39                write!(f, "hook '{}' auto-disabled after {} consecutive traps", name, consecutive_traps)
40            }
41            HookError::InvalidHookPoint(msg) => write!(f, "invalid hook point: {}", msg),
42            HookError::IoError(e) => write!(f, "I/O error: {}", e),
43            HookError::AbiVersionMismatch { hook_name, expected, found } => {
44                match found {
45                    Some(v) => write!(
46                        f,
47                        "hook '{}' ABI version mismatch: host expects {}, hook has {}. \
48                         Recompile the hook against the current rns-hooks-sdk.",
49                        hook_name, expected, v
50                    ),
51                    None => write!(
52                        f,
53                        "hook '{}' missing __rns_abi_version export (expected ABI version {}). \
54                         Recompile the hook against the current rns-hooks-sdk.",
55                        hook_name, expected
56                    ),
57                }
58            }
59        }
60    }
61}
62
63impl std::error::Error for HookError {}
64
65impl From<std::io::Error> for HookError {
66    fn from(e: std::io::Error) -> Self {
67        HookError::IoError(e)
68    }
69}