fluxtokens 1.0.0

Official FluxTokens Rust SDK - Access GPT-4.1, Gemini 2.5 and more at 30% lower cost
Documentation
use thiserror::Error;

/// Error type for FluxTokens API
#[derive(Error, Debug)]
pub enum Error {
    /// Authentication error (401)
    #[error("Authentication error: {0}")]
    Authentication(String),

    /// Rate limit exceeded (429)
    #[error("Rate limit exceeded: {0}")]
    RateLimit(String),

    /// Insufficient balance (402)
    #[error("Insufficient balance: {0}")]
    InsufficientBalance(String),

    /// Bad request (400)
    #[error("Bad request: {0}")]
    BadRequest(String),

    /// Internal server error (500+)
    #[error("Internal server error: {0}")]
    InternalServer(String),

    /// HTTP error
    #[error("HTTP error: {0}")]
    Http(#[from] reqwest::Error),

    /// JSON serialization/deserialization error
    #[error("JSON error: {0}")]
    Json(#[from] serde_json::Error),

    /// Timeout error
    #[error("Request timed out")]
    Timeout,

    /// Connection error
    #[error("Connection failed: {0}")]
    Connection(String),

    /// Unknown error
    #[error("Unknown error: {0}")]
    Unknown(String),
}

impl Error {
    /// Check if this is an authentication error
    pub fn is_authentication(&self) -> bool {
        matches!(self, Error::Authentication(_))
    }

    /// Check if this is a rate limit error
    pub fn is_rate_limit(&self) -> bool {
        matches!(self, Error::RateLimit(_))
    }

    /// Check if this is an insufficient balance error
    pub fn is_insufficient_balance(&self) -> bool {
        matches!(self, Error::InsufficientBalance(_))
    }

    /// Check if this is a bad request error
    pub fn is_bad_request(&self) -> bool {
        matches!(self, Error::BadRequest(_))
    }

    /// Check if this is a server error
    pub fn is_server_error(&self) -> bool {
        matches!(self, Error::InternalServer(_))
    }

    /// Check if this error is retryable
    pub fn is_retryable(&self) -> bool {
        matches!(self, Error::RateLimit(_) | Error::InternalServer(_) | Error::Timeout)
    }
}