ai_providers/utils/
errors.rs

1use std::fmt;
2
3#[derive(Debug)]
4pub enum ProviderError {
5    NetworkError(String),
6    ApiError { status: u16, message: String },
7    DeserializationError(String),
8    ValidationError(String),
9    CapabilityError(String),
10    NotSupported(String),
11    InternalError(String),
12    Other(String),
13}
14
15impl fmt::Display for ProviderError {
16    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
17        match self {
18            ProviderError::NetworkError(msg) => write!(f, "Network error: {}", msg),
19            ProviderError::ApiError { status, message } => {
20                write!(f, "API error (status {}): {}", status, message)
21            }
22            ProviderError::DeserializationError(msg) => {
23                write!(f, "Deserialization error: {}", msg)
24            }
25            ProviderError::ValidationError(msg) => write!(f, "Validation error: {}", msg),
26            ProviderError::CapabilityError(msg) => write!(f, "Capability error: {}", msg),
27            ProviderError::NotSupported(msg) => write!(f, "Operation not supported: {}", msg),
28            ProviderError::InternalError(msg) => write!(f, "Internal provider error: {}", msg),
29            ProviderError::Other(msg) => write!(f, "An unexpected error occurred: {}", msg),
30        }
31    }
32}
33
34impl std::error::Error for ProviderError {
35    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
36        match self {
37            ProviderError::NetworkError(_) => None,
38            ProviderError::ApiError { .. } => None,
39            ProviderError::DeserializationError(_) => None,
40            ProviderError::ValidationError(_) => None,
41            ProviderError::CapabilityError(_) => None,
42            ProviderError::NotSupported(_) => None,
43            ProviderError::InternalError(_) => None,
44            ProviderError::Other(_) => None,
45        }
46    }
47}