pixeluvw_supabase 0.1.0

A production-ready, high-performance Supabase SDK for Rust with middleware, retry logic, and Arc<Inner> architecture
Documentation
//! Error types for the Supabase SDK.
//!
//! This module provides a unified error type ([`SupaError`]) for all SDK operations.
//!
//! # Error Handling Example
//!
//! ```rust,no_run
//! use pixeluvw_supabase::{SupabaseClient, SupaError};
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! dotenv::dotenv().ok();
//!
//! let client = match SupabaseClient::from_env() {
//!     Ok(c) => c,
//!     Err(SupaError::ClientError { message }) => {
//!         eprintln!("Configuration error: {}", message);
//!         return Err(message.into());
//!     }
//!     Err(e) => return Err(e.into()),
//! };
//!
//! match client.from("users").select("*").execute::<Vec<serde_json::Value>>().await {
//!     Ok(users) => println!("Found {} users", users.len()),
//!     Err(SupaError::ApiError { message, .. }) => eprintln!("API error: {}", message),
//!     Err(SupaError::RequestError(e)) => eprintln!("Network error: {}", e),
//!     Err(e) => eprintln!("Other error: {:?}", e),
//! }
//! # Ok(())
//! # }
//! ```

use thiserror::Error;

/// Unified error type for all Supabase SDK operations.
///
/// Use pattern matching to handle specific error cases:
///
/// ```rust
/// use pixeluvw_supabase::SupaError;
///
/// fn handle_error(err: SupaError) {
///     match err {
///         SupaError::ClientError { message } => println!("Config issue: {}", message),
///         SupaError::AuthError { message } => println!("Auth failed: {}", message),
///         SupaError::ApiError { message, code, .. } => println!("API error: {} ({})", message, code),
///         SupaError::RequestError(e) => println!("Network error: {}", e),
///         SupaError::JsonError(e) => println!("JSON parse error: {}", e),
///         _ => println!("Other error: {:?}", err),
///     }
/// }
/// ```
#[derive(Error, Debug)]
pub enum SupaError {
    /// HTTP request failed (network error, timeout, etc.)
    #[error("HTTP request failed: {0}")]
    RequestError(#[from] reqwest::Error),

    /// Supabase API returned an error response
    #[error("Supabase API error: {code} - {message}")]
    ApiError {
        code: u16,
        message: String,
        details: Option<String>,
    },

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

    /// URL parsing failed
    #[error("URL parsing error: {0}")]
    UrlError(#[from] url::ParseError),

    /// Authentication error
    #[error("Authentication failed: {message}")]
    AuthError { message: String },

    /// Client configuration error
    #[error("Client configuration error: {message}")]
    ClientError { message: String },

    /// Storage operation failed
    #[error("Storage error: {message}")]
    StorageError { message: String },

    /// Edge Function invocation failed
    #[error("Functions error: {message}")]
    FunctionsError { message: String },

    /// Realtime connection or subscription error
    #[error("Realtime error: {message}")]
    RealtimeError { message: String },
}

/// A specialized `Result` type for Supabase operations.
///
/// This is defined as `Result<T, SupaError>` and is used throughout the SDK.
pub type Result<T> = std::result::Result<T, SupaError>;