1use thiserror::Error;
6
7#[derive(Debug, Error)]
9pub enum Error {
10 #[error("Configuration error: {0}")]
12 ConfigError(String),
13
14 #[error("Environment variable error: {0}")]
16 EnvError(String),
17
18 #[error("Authentication error: {0}")]
20 AuthError(String),
21
22 #[error("Private key error: {0}")]
24 KeyError(String),
25
26 #[error("HTTP error: {0}")]
28 HttpError(#[from] reqwest::Error),
29
30 #[error("JSON error: {0}")]
32 JsonError(#[from] serde_json::Error),
33
34 #[error("API error (code: {code}): {message}")]
36 ApiError {
37 code: String,
39 message: String,
41 },
42
43 #[error("I/O error: {0}")]
45 IoError(#[from] std::io::Error),
46
47 #[error("INI file parsing error: {0}")]
49 IniError(String),
50
51 #[error("Other error: {0}")]
53 Other(String),
54}
55
56pub type Result<T> = std::result::Result<T, Error>;
58
59#[cfg(test)]
60mod tests {
61 use super::*;
62
63 #[test]
64 fn test_config_error() {
65 let error = Error::ConfigError("test message".to_string());
66 assert_eq!(error.to_string(), "Configuration error: test message");
67 }
68
69 #[test]
70 fn test_env_error() {
71 let error = Error::EnvError("OCI_USER_ID is not set".to_string());
72 assert_eq!(
73 error.to_string(),
74 "Environment variable error: OCI_USER_ID is not set"
75 );
76 }
77
78 #[test]
79 fn test_auth_error() {
80 let error = Error::AuthError("Failed to sign request".to_string());
81 assert_eq!(
82 error.to_string(),
83 "Authentication error: Failed to sign request"
84 );
85 }
86
87 #[test]
88 fn test_key_error() {
89 let error = Error::KeyError("Private key file not found".to_string());
90 assert_eq!(
91 error.to_string(),
92 "Private key error: Private key file not found"
93 );
94 }
95
96 #[test]
97 fn test_api_error() {
98 let error = Error::ApiError {
99 code: "404".to_string(),
100 message: "Resource not found".to_string(),
101 };
102 assert_eq!(
103 error.to_string(),
104 "API error (code: 404): Resource not found"
105 );
106 }
107
108 #[test]
109 fn test_ini_error() {
110 let error = Error::IniError("Failed to parse INI file".to_string());
111 assert_eq!(
112 error.to_string(),
113 "INI file parsing error: Failed to parse INI file"
114 );
115 }
116
117 #[test]
118 fn test_other_error() {
119 let error = Error::Other("Something went wrong".to_string());
120 assert_eq!(error.to_string(), "Other error: Something went wrong");
121 }
122
123 #[test]
124 fn test_from_io_error() {
125 let io_error = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
126 let error: Error = io_error.into();
127 assert!(matches!(error, Error::IoError(_)));
128 }
129
130 #[test]
131 fn test_from_serde_json_error() {
132 let json_error = serde_json::from_str::<serde_json::Value>("invalid json").unwrap_err();
133 let error: Error = json_error.into();
134 assert!(matches!(error, Error::JsonError(_)));
135 }
136
137 #[test]
138 fn test_result_type_alias() {
139 fn returns_result() -> Result<i32> {
140 Ok(42)
141 }
142
143 fn returns_error() -> Result<i32> {
144 Err(Error::ConfigError("test".to_string()))
145 }
146
147 assert_eq!(returns_result().unwrap(), 42);
148 assert!(returns_error().is_err());
149 }
150}