gglib_core/ports/huggingface/
error.rs

1//! Error types for `HuggingFace` port operations.
2
3use thiserror::Error;
4
5/// Errors from `HuggingFace` port operations.
6///
7/// These are domain-level errors that consumers can handle.
8/// Implementation-specific errors (HTTP, JSON) are mapped to these.
9#[derive(Debug, Error)]
10pub enum HfPortError {
11    /// The requested model was not found.
12    #[error("Model not found: {model_id}")]
13    ModelNotFound {
14        /// The model ID that wasn't found
15        model_id: String,
16    },
17
18    /// The requested quantization wasn't found for the model.
19    #[error("Quantization '{quantization}' not found for model '{model_id}'")]
20    QuantizationNotFound {
21        /// The model ID
22        model_id: String,
23        /// The quantization that wasn't found
24        quantization: String,
25    },
26
27    /// API rate limit exceeded.
28    #[error("Rate limit exceeded, try again later")]
29    RateLimited,
30
31    /// Authentication required or failed.
32    #[error("Authentication required for private model: {model_id}")]
33    AuthRequired {
34        /// The model ID that requires auth
35        model_id: String,
36    },
37
38    /// Network or connectivity error.
39    #[error("Network error: {message}")]
40    Network {
41        /// Description of the network error
42        message: String,
43    },
44
45    /// Invalid response from the API.
46    #[error("Invalid API response: {message}")]
47    InvalidResponse {
48        /// What was invalid
49        message: String,
50    },
51
52    /// Configuration error.
53    #[error("Configuration error: {message}")]
54    Configuration {
55        /// What's wrong with the configuration
56        message: String,
57    },
58}
59
60/// Result type alias for `HuggingFace` port operations.
61pub type HfPortResult<T> = Result<T, HfPortError>;
62
63#[cfg(test)]
64mod tests {
65    use super::*;
66
67    #[test]
68    fn test_error_display() {
69        let err = HfPortError::ModelNotFound {
70            model_id: "TheBloke/Test-GGUF".to_string(),
71        };
72        assert!(err.to_string().contains("TheBloke/Test-GGUF"));
73
74        let err = HfPortError::QuantizationNotFound {
75            model_id: "Org/Model".to_string(),
76            quantization: "Q4_K_M".to_string(),
77        };
78        assert!(err.to_string().contains("Q4_K_M"));
79        assert!(err.to_string().contains("Org/Model"));
80    }
81}