hanzo_guard/
error.rs

1//! Error types for Hanzo Guard
2
3use thiserror::Error;
4
5/// Result type alias for Guard operations
6pub type Result<T> = std::result::Result<T, GuardError>;
7
8/// Guard error types
9#[derive(Debug, Error)]
10pub enum GuardError {
11    /// Content was blocked due to safety policy
12    #[error("Content blocked: {reason} (category: {category:?})")]
13    ContentBlocked {
14        reason: String,
15        category: SafetyCategory,
16    },
17
18    /// Rate limit exceeded
19    #[error("Rate limit exceeded: {0}")]
20    RateLimitExceeded(String),
21
22    /// PII detection error
23    #[error("PII detection error: {0}")]
24    PiiDetectionError(String),
25
26    /// Injection detection error
27    #[error("Injection detection error: {0}")]
28    InjectionDetectionError(String),
29
30    /// Content filter API error
31    #[error("Content filter error: {0}")]
32    ContentFilterError(String),
33
34    /// Configuration error
35    #[error("Configuration error: {0}")]
36    ConfigError(String),
37
38    /// Serialization error
39    #[error("Serialization error: {0}")]
40    SerializationError(#[from] serde_json::Error),
41
42    /// IO error
43    #[error("IO error: {0}")]
44    IoError(#[from] std::io::Error),
45
46    /// HTTP error (when content filter is enabled)
47    #[cfg(feature = "content-filter")]
48    #[error("HTTP error: {0}")]
49    HttpError(#[from] reqwest::Error),
50}
51
52/// Safety categories aligned with Zen Guard
53#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
54pub enum SafetyCategory {
55    /// Violence and weapons
56    Violent,
57    /// Non-violent illegal activities
58    IllegalActs,
59    /// Sexual content
60    SexualContent,
61    /// Personally identifiable information
62    Pii,
63    /// Self-harm and suicide
64    SelfHarm,
65    /// Discrimination and hate speech
66    UnethicalActs,
67    /// Political misinformation
68    PoliticallySensitive,
69    /// Copyright infringement
70    CopyrightViolation,
71    /// Jailbreak attempts
72    Jailbreak,
73    /// No specific category
74    None,
75}
76
77impl std::fmt::Display for SafetyCategory {
78    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
79        match self {
80            SafetyCategory::Violent => write!(f, "Violent"),
81            SafetyCategory::IllegalActs => write!(f, "Non-violent Illegal Acts"),
82            SafetyCategory::SexualContent => write!(f, "Sexual Content"),
83            SafetyCategory::Pii => write!(f, "PII"),
84            SafetyCategory::SelfHarm => write!(f, "Suicide & Self-Harm"),
85            SafetyCategory::UnethicalActs => write!(f, "Unethical Acts"),
86            SafetyCategory::PoliticallySensitive => write!(f, "Politically Sensitive"),
87            SafetyCategory::CopyrightViolation => write!(f, "Copyright Violation"),
88            SafetyCategory::Jailbreak => write!(f, "Jailbreak"),
89            SafetyCategory::None => write!(f, "None"),
90        }
91    }
92}