syncable-ag-ui-client 0.2.0

Client-side AG-UI event consumer for Rust applications - Syncable SDK
Documentation
//! Error types for AG-UI client operations.

use thiserror::Error;

/// Errors that can occur during AG-UI client operations.
#[derive(Debug, Error)]
pub enum ClientError {
    /// Failed to connect to the server.
    #[error("connection failed: {0}")]
    Connection(String),

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

    /// WebSocket error.
    #[error("WebSocket error: {0}")]
    WebSocket(#[from] tokio_tungstenite::tungstenite::Error),

    /// Failed to parse event data.
    #[error("parse error: {0}")]
    Parse(String),

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

    /// SSE stream error.
    #[error("SSE error: {0}")]
    Sse(String),

    /// The connection was closed.
    #[error("connection closed")]
    Closed,

    /// Invalid URL provided.
    #[error("invalid URL: {0}")]
    InvalidUrl(String),

    /// State reconstruction error.
    #[error("state error: {0}")]
    State(String),
}

impl ClientError {
    /// Creates a new connection error.
    pub fn connection(msg: impl Into<String>) -> Self {
        Self::Connection(msg.into())
    }

    /// Creates a new parse error.
    pub fn parse(msg: impl Into<String>) -> Self {
        Self::Parse(msg.into())
    }

    /// Creates a new SSE error.
    pub fn sse(msg: impl Into<String>) -> Self {
        Self::Sse(msg.into())
    }

    /// Creates a new state error.
    pub fn state(msg: impl Into<String>) -> Self {
        Self::State(msg.into())
    }
}

/// Result type for client operations.
pub type Result<T> = std::result::Result<T, ClientError>;

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_error_display() {
        let err = ClientError::connection("refused");
        assert_eq!(format!("{}", err), "connection failed: refused");

        let err = ClientError::parse("invalid JSON");
        assert_eq!(format!("{}", err), "parse error: invalid JSON");

        let err = ClientError::Closed;
        assert_eq!(format!("{}", err), "connection closed");
    }

    #[test]
    fn test_error_constructors() {
        let err = ClientError::connection("test");
        assert!(matches!(err, ClientError::Connection(_)));

        let err = ClientError::sse("stream ended");
        assert!(matches!(err, ClientError::Sse(_)));

        let err = ClientError::state("invalid patch");
        assert!(matches!(err, ClientError::State(_)));
    }
}