rocketmq_error/
auth_error.rs1use thiserror::Error;
18
19#[derive(Debug, Clone, Error)]
25pub enum AuthError {
26 #[error("Missing DateTime header: {0}")]
28 MissingDateTime(String),
29
30 #[error("Invalid Authorization header: {0}")]
32 InvalidAuthorizationHeader(String),
33
34 #[error("Invalid credential: {0}")]
36 InvalidCredential(String),
37
38 #[error("Invalid hex signature: {0}")]
40 InvalidHexSignature(String),
41
42 #[error("Failed to create authentication context: {0}")]
44 ContextCreationError(String),
45
46 #[error("Authentication failed: {0}")]
48 AuthenticationFailed(String),
49
50 #[error("User not found: {0}")]
52 UserNotFound(String),
53
54 #[error("Invalid signature: {0}")]
56 InvalidSignature(String),
57
58 #[error("Invalid user status: {0}")]
60 InvalidUserStatus(String),
61
62 #[error("Authentication error: {0}")]
64 Other(String),
65}
66
67impl From<String> for AuthError {
68 fn from(msg: String) -> Self {
69 AuthError::Other(msg)
70 }
71}
72
73impl From<&str> for AuthError {
74 fn from(msg: &str) -> Self {
75 AuthError::Other(msg.to_string())
76 }
77}
78
79#[cfg(test)]
80mod tests {
81 use super::*;
82
83 #[test]
84 fn test_auth_error_variants() {
85 let errors = vec![
86 AuthError::AuthenticationFailed("could not authenticate".to_string()),
87 AuthError::ContextCreationError("could not create context".to_string()),
88 AuthError::InvalidAuthorizationHeader("invalid authorization header".to_string()),
89 AuthError::InvalidCredential("invalid credential".to_string()),
90 AuthError::InvalidHexSignature("invalid hex signature".to_string()),
91 AuthError::InvalidSignature("invalid signature".to_string()),
92 AuthError::InvalidUserStatus("invalid user status".to_string()),
93 AuthError::MissingDateTime("missing date time".to_string()),
94 AuthError::Other("other error".to_string()),
95 AuthError::UserNotFound("user not found".to_string()),
96 ];
97
98 for error in errors {
99 let _msg = format!("{}", error);
100 let _debug = format!("{:?}", error);
101 }
102 }
103
104 #[test]
105 fn test_from_string_creates_other() {
106 let error: AuthError = String::from("custom error").into();
107
108 match error {
109 AuthError::Other(msg) => assert_eq!(msg, "custom error"),
110 _ => panic!("Expected AuthError::Other"),
111 }
112 }
113
114 #[test]
115 fn test_from_str_creates_other() {
116 let error: AuthError = "custom error".into();
117
118 match error {
119 AuthError::Other(msg) => assert_eq!(msg, "custom error"),
120 _ => panic!("Expected AuthError::Other"),
121 }
122 }
123}