Skip to main content

edgequake_llm/imagegen/
error.rs

1//! Errors for image generation providers.
2
3use std::time::Duration;
4
5use thiserror::Error;
6
7/// Result type for image generation operations.
8pub type Result<T> = std::result::Result<T, ImageGenError>;
9
10/// Errors returned by image generation providers.
11#[derive(Debug, Error)]
12pub enum ImageGenError {
13    #[error("authentication error: {0}")]
14    AuthError(String),
15
16    #[error("configuration error: {0}")]
17    ConfigError(String),
18
19    #[error("invalid request: {0}")]
20    InvalidRequest(String),
21
22    #[error("invalid response: {0}")]
23    InvalidResponse(String),
24
25    #[error("provider error: {0}")]
26    ProviderError(String),
27
28    #[error("network error: {0}")]
29    NetworkError(String),
30
31    #[error("content filtered: {reason}")]
32    ContentFiltered { reason: String },
33
34    #[error("rate limited")]
35    RateLimited { retry_after: Option<Duration> },
36
37    #[error("request timed out after {elapsed_secs}s")]
38    Timeout { elapsed_secs: u64 },
39
40    #[error("not supported: {0}")]
41    NotSupported(String),
42
43    #[error("serialization error: {0}")]
44    SerializationError(#[from] serde_json::Error),
45}
46
47impl From<reqwest::Error> for ImageGenError {
48    fn from(err: reqwest::Error) -> Self {
49        if err.is_timeout() {
50            Self::Timeout { elapsed_secs: 0 }
51        } else if err.is_connect() {
52            Self::NetworkError(format!("connection failed: {err}"))
53        } else {
54            Self::NetworkError(err.to_string())
55        }
56    }
57}
58
59impl From<base64::DecodeError> for ImageGenError {
60    fn from(err: base64::DecodeError) -> Self {
61        Self::InvalidResponse(format!("invalid base64 image payload: {err}"))
62    }
63}