orion_sec/
error.rs

1use derive_more::From;
2use orion_error::{ErrorCode, StructError, UvsReason};
3use serde_derive::Serialize;
4use thiserror::Error;
5
6#[derive(Debug, PartialEq, Serialize, Error, From)]
7pub enum OrionSecReason {
8    #[error("{0}")]
9    Sec(SecReason),
10    #[error("{0}")]
11    Uvs(UvsReason),
12}
13
14#[derive(Debug, PartialEq, Serialize, Error)]
15pub enum SecReason {
16    #[error("sensitive msg {0}")]
17    SensitiveMsg(String),
18    #[error("no permission {0}")]
19    NoPermission(String),
20    #[error("deception {0}")]
21    Deception(String),
22    #[error("un authenticated {0}")]
23    UnAuthenticated(String),
24}
25
26pub type SecError = StructError<OrionSecReason>;
27pub type SecResult<T> = Result<T, SecError>;
28
29impl ErrorCode for OrionSecReason {
30    fn error_code(&self) -> i32 {
31        match self {
32            OrionSecReason::Sec(sec_reason) => match sec_reason {
33                SecReason::SensitiveMsg(_) => 101,
34                SecReason::NoPermission(_) => 201,
35                SecReason::Deception(_) => 301,
36                SecReason::UnAuthenticated(_) => 401,
37            },
38            OrionSecReason::Uvs(u) => u.error_code(),
39        }
40    }
41}
42#[cfg(test)]
43mod tests {
44    use super::*;
45    use orion_conf::ToStructError;
46    use orion_error::{ErrorConv, ErrorOwe, UvsPermissionFrom};
47
48    #[derive(Debug, PartialEq, Serialize, Error, From)]
49    pub enum TargetReason {
50        #[error("{0}")]
51        Uvs(UvsReason),
52    }
53
54    pub type TargetError = StructError<TargetReason>;
55
56    impl From<OrionSecReason> for TargetReason {
57        fn from(value: OrionSecReason) -> Self {
58            match value {
59                OrionSecReason::Sec(_) => Self::Uvs(UvsReason::from_permission("sec error")),
60                OrionSecReason::Uvs(u) => Self::Uvs(u),
61            }
62        }
63    }
64    fn err_fun() -> SecResult<()> {
65        OrionSecReason::Sec(SecReason::NoPermission("no perm".to_string())).err_result()
66    }
67    fn call_err() -> Result<(), TargetError> {
68        err_fun().err_conv()
69    }
70
71    #[test]
72    fn test_err_conv() {
73        match call_err() {
74            Ok(_) => todo!(),
75            Err(e) => {
76                println!("{:#?}", e);
77            }
78        }
79    }
80
81    #[test]
82    fn test_sec_reason_sensitive_msg() {
83        let reason = SecReason::SensitiveMsg("test message".to_string());
84        assert_eq!(format!("{}", reason), "sensitive msg test message");
85    }
86
87    #[test]
88    fn test_sec_error_creation() {
89        let reason = OrionSecReason::Sec(SecReason::SensitiveMsg("operation failed".to_string()));
90        let error: SecError = Result::<(), OrionSecReason>::Err(reason)
91            .owe_logic()
92            .unwrap_err();
93
94        let debug_str = format!("{:?}", error);
95        assert!(debug_str.contains("operation failed"));
96    }
97
98    #[test]
99    fn test_sec_result_ok() {
100        fn successful_operation() -> SecResult<i32> {
101            Ok(42)
102        }
103
104        let result = successful_operation();
105        assert!(result.is_ok());
106        assert_eq!(result.unwrap(), 42);
107    }
108
109    #[test]
110    fn test_sec_result_err() {
111        fn failing_operation() -> SecResult<i32> {
112            let reason = OrionSecReason::Sec(SecReason::SensitiveMsg("not found".to_string()));
113            Result::<i32, OrionSecReason>::Err(reason).owe_logic()
114        }
115
116        let result = failing_operation();
117        assert!(result.is_err());
118    }
119
120    #[test]
121    fn test_orion_sec_reason_equality() {
122        let reason1 = OrionSecReason::Sec(SecReason::SensitiveMsg("a".to_string()));
123        let reason2 = OrionSecReason::Sec(SecReason::SensitiveMsg("a".to_string()));
124        let reason3 = OrionSecReason::Sec(SecReason::SensitiveMsg("b".to_string()));
125
126        assert_eq!(reason1, reason2);
127        assert_ne!(reason1, reason3);
128    }
129
130    #[test]
131    fn test_sec_reason_serializable() {
132        let reason = SecReason::SensitiveMsg("serialize me".to_string());
133        let serialized = serde_yaml::to_string(&reason);
134        assert!(serialized.is_ok());
135        assert!(serialized.unwrap().contains("serialize me"));
136    }
137
138    #[test]
139    fn test_orion_sec_reason_serializable() {
140        // Only test SecReason variant as UvsReason may have different serialization behavior
141        let reason = OrionSecReason::Sec(SecReason::SensitiveMsg("data".to_string()));
142        let debug_str = format!("{:?}", reason);
143        assert!(debug_str.contains("data"));
144    }
145
146    #[test]
147    fn test_sec_error_display() {
148        let reason = OrionSecReason::Sec(SecReason::SensitiveMsg("password leaked".to_string()));
149        let error: SecError = reason.to_err();
150
151        // SecError uses Debug format for display
152        let display_str = format!("{}", error);
153        println!("{}", display_str);
154        assert!(display_str.contains("password leaked"));
155        assert_eq!(error.error_code(), 101);
156    }
157}