scoutquest_rust/
error.rs

1use thiserror::Error;
2
3/// Errors that can occur when using the ScoutQuest Rust SDK.
4///
5/// This enum covers all possible error conditions including network failures,
6/// service discovery issues, and protocol-level errors.
7#[derive(Error, Debug)]
8pub enum ScoutQuestError {
9    /// Network-related errors (connection failures, timeouts, etc.)
10    #[error("Network error: {0}")]
11    NetworkError(#[from] reqwest::Error),
12
13    /// The requested service was not found in the discovery registry
14    #[error("Service isn't found: {service_name}")]
15    ServiceNotFound { service_name: String },
16
17    /// A specific service instance was not found
18    #[error("Instance isn't found: {instance_id}")]
19    InstanceNotFound { instance_id: String },
20
21    /// Service registration failed with the discovery server
22    #[error("Registration failed: {status} - {message}")]
23    RegistrationFailed { status: u16, message: String },
24
25    /// JSON serialization/deserialization error
26    #[error("Serialization error: {0}")]
27    SerializationError(#[from] serde_json::Error),
28
29    /// Invalid URL format provided
30    #[error("Invalid URL: {0}")]
31    InvalidUrl(#[from] url::ParseError),
32
33    /// The ScoutQuest discovery server is not available
34    #[error("ScoutQuest Server unavailable")]
35    ServerUnavailable,
36
37    /// Operation timed out
38    #[error("Operation timeout")]
39    Timeout,
40
41    /// No healthy instances are available for the requested service
42    #[error("No healthy instances available for service: {service_name}")]
43    NoHealthyInstances { service_name: String },
44
45    /// Internal error or unexpected condition
46    #[error("Internal error: {0}")]
47    InternalError(String),
48}
49
50/// Convenience type alias for Results in the ScoutQuest SDK.
51pub 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}