Skip to main content

orcs_hook/
error.rs

1//! Error types for the hook system.
2
3use thiserror::Error;
4
5/// Errors that can occur in the hook system.
6#[derive(Debug, Clone, PartialEq, Eq, Error)]
7pub enum HookError {
8    /// Invalid FQL pattern syntax.
9    #[error("invalid FQL pattern: {0}")]
10    InvalidFql(String),
11
12    /// Unknown hook point string.
13    #[error("unknown hook point: {0}")]
14    UnknownHookPoint(String),
15
16    /// Hook execution failed.
17    #[error("hook execution failed [{hook_id}]: {message}")]
18    ExecutionFailed {
19        /// ID of the hook that failed.
20        hook_id: String,
21        /// Error message.
22        message: String,
23    },
24
25    /// Hook with the given ID was not found.
26    #[error("hook not found: {0}")]
27    NotFound(String),
28
29    /// Hook chain depth limit exceeded.
30    #[error("depth limit exceeded (depth={depth}, max={max_depth})")]
31    DepthExceeded {
32        /// Current depth.
33        depth: u8,
34        /// Maximum allowed depth.
35        max_depth: u8,
36    },
37}
38
39#[cfg(test)]
40mod tests {
41    use super::*;
42
43    #[test]
44    fn display_invalid_fql() {
45        let err = HookError::InvalidFql("missing ::".into());
46        assert_eq!(err.to_string(), "invalid FQL pattern: missing ::");
47    }
48
49    #[test]
50    fn display_unknown_hook_point() {
51        let err = HookError::UnknownHookPoint("foo.bar".into());
52        assert_eq!(err.to_string(), "unknown hook point: foo.bar");
53    }
54
55    #[test]
56    fn display_execution_failed() {
57        let err = HookError::ExecutionFailed {
58            hook_id: "audit-hook".into(),
59            message: "lua error".into(),
60        };
61        assert_eq!(
62            err.to_string(),
63            "hook execution failed [audit-hook]: lua error"
64        );
65    }
66
67    #[test]
68    fn display_not_found() {
69        let err = HookError::NotFound("my-hook".into());
70        assert_eq!(err.to_string(), "hook not found: my-hook");
71    }
72
73    #[test]
74    fn display_depth_exceeded() {
75        let err = HookError::DepthExceeded {
76            depth: 5,
77            max_depth: 4,
78        };
79        assert_eq!(err.to_string(), "depth limit exceeded (depth=5, max=4)");
80    }
81
82    #[test]
83    fn error_is_clone_and_eq() {
84        let a = HookError::NotFound("x".into());
85        let b = a.clone();
86        assert_eq!(a, b);
87    }
88}