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 {
18        name: String,
19        consecutive_traps: u32,
20    },
21    /// Hook point not found or invalid.
22    InvalidHookPoint(String),
23    /// I/O error loading a WASM module.
24    IoError(std::io::Error),
25    /// WASM module was compiled against an incompatible ABI version.
26    AbiVersionMismatch {
27        hook_name: String,
28        expected: i32,
29        found: Option<i32>,
30    },
31}
32
33impl fmt::Display for HookError {
34    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35        match self {
36            HookError::CompileError(msg) => write!(f, "compile error: {}", msg),
37            HookError::InstantiationError(msg) => write!(f, "instantiation error: {}", msg),
38            HookError::FuelExhausted => write!(f, "fuel exhausted"),
39            HookError::Trap(msg) => write!(f, "trap: {}", msg),
40            HookError::InvalidResult(msg) => write!(f, "invalid result: {}", msg),
41            HookError::AutoDisabled {
42                name,
43                consecutive_traps,
44            } => {
45                write!(
46                    f,
47                    "hook '{}' auto-disabled after {} consecutive traps",
48                    name, consecutive_traps
49                )
50            }
51            HookError::InvalidHookPoint(msg) => write!(f, "invalid hook point: {}", msg),
52            HookError::IoError(e) => write!(f, "I/O error: {}", e),
53            HookError::AbiVersionMismatch {
54                hook_name,
55                expected,
56                found,
57            } => match found {
58                Some(v) => write!(
59                    f,
60                    "hook '{}' ABI version mismatch: host expects {}, hook has {}. \
61                         Recompile the hook against the current rns-hooks-sdk.",
62                    hook_name, expected, v
63                ),
64                None => write!(
65                    f,
66                    "hook '{}' missing __rns_abi_version export (expected ABI version {}). \
67                         Recompile the hook against the current rns-hooks-sdk.",
68                    hook_name, expected
69                ),
70            },
71        }
72    }
73}
74
75impl std::error::Error for HookError {}
76
77impl From<std::io::Error> for HookError {
78    fn from(e: std::io::Error) -> Self {
79        HookError::IoError(e)
80    }
81}