1use thiserror::Error;
2
3#[derive(Error, Debug)]
8pub enum ScoutQuestError {
9 #[error("Network error: {0}")]
11 NetworkError(#[from] reqwest::Error),
12
13 #[error("Service isn't found: {service_name}")]
15 ServiceNotFound { service_name: String },
16
17 #[error("Instance isn't found: {instance_id}")]
19 InstanceNotFound { instance_id: String },
20
21 #[error("Registration failed: {status} - {message}")]
23 RegistrationFailed { status: u16, message: String },
24
25 #[error("Serialization error: {0}")]
27 SerializationError(#[from] serde_json::Error),
28
29 #[error("Invalid URL: {0}")]
31 InvalidUrl(#[from] url::ParseError),
32
33 #[error("ScoutQuest Server unavailable")]
35 ServerUnavailable,
36
37 #[error("Operation timeout")]
39 Timeout,
40
41 #[error("No healthy instances available for service: {service_name}")]
43 NoHealthyInstances { service_name: String },
44
45 #[error("Internal error: {0}")]
47 InternalError(String),
48}
49
50pub type Result<T> = std::result::Result<T, ScoutQuestError>;
52
53#[cfg(test)]
54mod tests {
55 use super::*;
56
57 #[test]
58 fn test_error_display() {
59 let error = ScoutQuestError::ServiceNotFound {
60 service_name: "test-service".to_string(),
61 };
62 assert_eq!(error.to_string(), "Service isn't found: test-service");
63
64 let error = ScoutQuestError::InstanceNotFound {
65 instance_id: "instance-123".to_string(),
66 };
67 assert_eq!(error.to_string(), "Instance isn't found: instance-123");
68
69 let error = ScoutQuestError::RegistrationFailed {
70 status: 500,
71 message: "Internal server error".to_string(),
72 };
73 assert_eq!(
74 error.to_string(),
75 "Registration failed: 500 - Internal server error"
76 );
77
78 let error = ScoutQuestError::NoHealthyInstances {
79 service_name: "api-service".to_string(),
80 };
81 assert_eq!(
82 error.to_string(),
83 "No healthy instances available for service: api-service"
84 );
85
86 let error = ScoutQuestError::InternalError("Something went wrong".to_string());
87 assert_eq!(error.to_string(), "Internal error: Something went wrong");
88
89 let error = ScoutQuestError::ServerUnavailable;
90 assert_eq!(error.to_string(), "ScoutQuest Server unavailable");
91
92 let error = ScoutQuestError::Timeout;
93 assert_eq!(error.to_string(), "Operation timeout");
94 }
95
96 #[test]
97 fn test_error_debug() {
98 let error = ScoutQuestError::ServiceNotFound {
99 service_name: "test".to_string(),
100 };
101 let debug_str = format!("{:?}", error);
102 assert!(debug_str.contains("ServiceNotFound"));
103 assert!(debug_str.contains("test"));
104 }
105}