use std::fmt;
#[derive(Debug)]
pub enum HookError {
CompileError(String),
InstantiationError(String),
FuelExhausted,
Trap(String),
InvalidResult(String),
AutoDisabled {
name: String,
consecutive_traps: u32,
},
InvalidHookPoint(String),
IoError(std::io::Error),
AbiVersionMismatch {
hook_name: String,
expected: i32,
found: Option<i32>,
},
}
impl fmt::Display for HookError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
HookError::CompileError(msg) => write!(f, "compile error: {}", msg),
HookError::InstantiationError(msg) => write!(f, "instantiation error: {}", msg),
HookError::FuelExhausted => write!(f, "fuel exhausted"),
HookError::Trap(msg) => write!(f, "trap: {}", msg),
HookError::InvalidResult(msg) => write!(f, "invalid result: {}", msg),
HookError::AutoDisabled {
name,
consecutive_traps,
} => {
write!(
f,
"hook '{}' auto-disabled after {} consecutive traps",
name, consecutive_traps
)
}
HookError::InvalidHookPoint(msg) => write!(f, "invalid hook point: {}", msg),
HookError::IoError(e) => write!(f, "I/O error: {}", e),
HookError::AbiVersionMismatch {
hook_name,
expected,
found,
} => match found {
Some(v) => write!(
f,
"hook '{}' ABI version mismatch: host expects {}, hook has {}. \
Recompile the hook against the current rns-hooks-sdk.",
hook_name, expected, v
),
None => write!(
f,
"hook '{}' missing __rns_abi_version export (expected ABI version {}). \
Recompile the hook against the current rns-hooks-sdk.",
hook_name, expected
),
},
}
}
}
impl std::error::Error for HookError {}
impl From<std::io::Error> for HookError {
fn from(e: std::io::Error) -> Self {
HookError::IoError(e)
}
}