opencrates 3.0.1

Enterprise-grade AI-powered Rust development companion with comprehensive automation, monitoring, and deployment capabilities
//! Error handling for OpenCrates

// use std::fmt; // This was reported as unused
use thiserror::Error; // Restored this essential import

#[derive(Error, Debug)]
// TODO: document this
// TODO: document this
// TODO: document this
pub enum OpenCratesError {
    #[error("Configuration error: {0}")]
    // TODO: document this
    Config(String),

    #[error("Template error: {0}")]
    // TODO: document this
    Template(String),

    #[error("Provider error: {0}")]
    // TODO: document this
    Provider(String),

    #[error("Search error: {0}")]
    // TODO: document this
    Search(String),

    #[error("Cache error: {0}")]
    // TODO: document this
    Cache(String),

    // TODO: Re-enable this when sqlx vulnerability is patched.
    // #[error("Database error: {0}")]
    // Database(#[from] sqlx::Error),
    #[error("Resource '{resource}' with id '{id}' not found")]
    // TODO: document this
    // TODO: document this
    NotFound { resource: String, id: String },

    #[error("Internal error: {0}")]
    // TODO: document this
    Internal(String),

    #[error("External error: {0}")]
    // TODO: document this
    External(String),

    #[error("IO error: {0}")]
    // TODO: document this
    Io(#[from] std::io::Error),

    #[error("Serialization error: {0}")]
    // TODO: document this
    Serialization(#[from] serde_json::Error),

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

    #[error("Redis error: {0}")]
    // TODO: document this
    Redis(String),

    #[error("Validation error: {0}")]
    // TODO: document this
    Validation(String),

    #[error("Authentication error: {0}")]
    // TODO: document this
    Auth(String),

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

    #[error("Network error: {0}")]
    // TODO: document this
    Network(String),

    #[error("Parse error: {0}")]
    // TODO: document this
    Parse(String),

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

impl OpenCratesError {
    // TODO: document this
    // TODO: document this
    // TODO: document this
    // TODO: document this
    pub fn config(message: impl Into<String>) -> Self {
        Self::Config(message.into())
    }

    // TODO: document this

    // TODO: document this

    // TODO: document this

    // TODO: document this

    pub fn template(template_name: impl Into<String>) -> Self {
        Self::Template(format!("Template '{}' not found", template_name.into()))
    }

    // TODO: document this

    // TODO: document this

    // TODO: document this

    // TODO: document this

    pub fn provider(provider: impl Into<String>) -> Self {
        Self::Provider(format!("Provider '{}' error", provider.into()))
    }

    // TODO: document this

    // TODO: document this

    // TODO: document this

    // TODO: document this

    pub fn search(message: impl Into<String>) -> Self {
        Self::Search(message.into())
    }

    // TODO: document this

    // TODO: document this

    // TODO: document this

    // TODO: document this

    pub fn cache(message: impl Into<String>) -> Self {
        Self::Cache(message.into())
    }

    // TODO: document this

    // TODO: document this

    // TODO: document this

    // TODO: document this

    pub fn not_found(resource: impl Into<String>, id: impl Into<String>) -> Self {
        Self::NotFound {
            resource: resource.into(),
            id: id.into(),
        }
    }

    // TODO: document this

    // TODO: document this

    // TODO: document this

    // TODO: document this

    pub fn internal(message: impl Into<String>) -> Self {
        Self::Internal(message.into())
    }

    // TODO: document this

    // TODO: document this

    // TODO: document this

    // TODO: document this

    pub fn external(message: impl Into<String>) -> Self {
        Self::External(message.into())
    }

    // TODO: document this

    // TODO: document this

    // TODO: document this

    // TODO: document this

    pub fn redis(message: impl Into<String>) -> Self {
        Self::Redis(message.into())
    }

    // TODO: document this

    // TODO: document this

    // TODO: document this

    // TODO: document this

    pub fn validation(message: impl Into<String>) -> Self {
        Self::Validation(message.into())
    }

    // TODO: document this

    // TODO: document this

    // TODO: document this

    // TODO: document this

    pub fn auth(message: impl Into<String>) -> Self {
        Self::Auth(message.into())
    }

    // TODO: document this

    // TODO: document this

    // TODO: document this

    // TODO: document this

    pub fn rate_limit(message: impl Into<String>) -> Self {
        Self::RateLimit(message.into())
    }

    // TODO: document this

    // TODO: document this

    // TODO: document this

    // TODO: document this

    pub fn network(message: impl Into<String>) -> Self {
        Self::Network(message.into())
    }

    // TODO: document this

    // TODO: document this

    // TODO: document this

    // TODO: document this

    pub fn parse(message: impl Into<String>) -> Self {
        Self::Parse(message.into())
    }

    // TODO: document this

    // TODO: document this

    // TODO: document this

    // TODO: document this

    pub fn unknown(message: impl Into<String>) -> Self {
        Self::Unknown(message.into())
    }
}

// Convert from redis errors
#[cfg(feature = "redis")]
impl From<redis::RedisError> for OpenCratesError {
    fn from(error: redis::RedisError) -> Self {
        Self::Redis(error.to_string())
    }
}

// Convert from anyhow errors
impl From<anyhow::Error> for OpenCratesError {
    fn from(error: anyhow::Error) -> Self {
        Self::External(error.to_string())
    }
}