sprites 0.1.0

Official Rust SDK for Sprites - stateful sandbox environments from Fly.io
Documentation
//! Error types for the Sprites SDK

use thiserror::Error;

/// Result type alias for Sprites operations
pub type Result<T> = std::result::Result<T, Error>;

/// Errors that can occur when interacting with the Sprites API
#[derive(Error, Debug)]
pub enum Error {
    /// HTTP request failed
    #[error("HTTP request failed: {0}")]
    Http(#[from] reqwest::Error),

    /// WebSocket error (boxed to reduce enum size)
    #[error("WebSocket error: {0}")]
    WebSocket(#[source] Box<tokio_tungstenite::tungstenite::Error>),

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

    /// API returned an error response
    #[error("API error ({status}): {message}")]
    Api {
        status: u16,
        message: String,
    },

    /// Sprite not found
    #[error("Sprite not found: {0}")]
    NotFound(String),

    /// Invalid URL
    #[error("Invalid URL: {0}")]
    InvalidUrl(#[from] url::ParseError),

    /// Command execution failed
    #[error("Command failed with exit code {code}: {message}")]
    ExecFailed {
        code: i32,
        message: String,
    },

    /// Timeout waiting for operation
    #[error("Operation timed out")]
    Timeout,

    /// Invalid response from server
    #[error("Invalid response: {0}")]
    InvalidResponse(String),

    /// Connection error (e.g., failed to bind to port)
    #[error("Connection error: {0}")]
    Connection(String),

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

impl From<tokio_tungstenite::tungstenite::Error> for Error {
    fn from(err: tokio_tungstenite::tungstenite::Error) -> Self {
        Self::WebSocket(Box::new(err))
    }
}

impl Error {
    /// Create a connection error
    pub fn connection(message: impl Into<String>) -> Self {
        Self::Connection(message.into())
    }
    /// Create an API error from status code and message
    pub fn api(status: u16, message: impl Into<String>) -> Self {
        Self::Api {
            status,
            message: message.into(),
        }
    }

    /// Create a not found error
    pub fn not_found(name: impl Into<String>) -> Self {
        Self::NotFound(name.into())
    }

    /// Create an exec failed error
    pub fn exec_failed(code: i32, message: impl Into<String>) -> Self {
        Self::ExecFailed {
            code,
            message: message.into(),
        }
    }
}