ostium-rust-sdk 0.1.0

Rust SDK for interacting with the Ostium trading platform on Arbitrum
Documentation
//! Error types for the Ostium SDK
//!
//! This module defines all error types that can occur when using the SDK.

use thiserror::Error;

/// Type alias for Results returned by the SDK
pub type Result<T> = std::result::Result<T, OstiumError>;

/// Main error type for the Ostium SDK
#[derive(Error, Debug)]
pub enum OstiumError {
    /// Network-related errors (RPC, connection issues)
    #[error("Network error: {0}")]
    Network(String),

    /// Smart contract interaction errors
    #[error("Contract error: {0}")]
    Contract(String),

    /// GraphQL API errors
    #[error("GraphQL error: {0}")]
    GraphQL(String),

    /// Configuration errors
    #[error("Configuration error: {0}")]
    Config(String),

    /// Validation errors for input parameters
    #[error("Validation error: {0}")]
    Validation(String),

    /// Errors related to signing and wallet operations
    #[error("Wallet error: {0}")]
    Wallet(String),

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

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

    /// Decimal conversion errors
    #[error("Decimal error: {0}")]
    Decimal(#[from] rust_decimal::Error),

    /// Alloy provider errors
    #[error("Provider error: {0}")]
    Provider(String),

    /// Generic errors with context
    #[error("{0}")]
    Other(String),
}

impl OstiumError {
    /// Create a new network error
    pub fn network<S: Into<String>>(msg: S) -> Self {
        Self::Network(msg.into())
    }

    /// Create a new contract error
    pub fn contract<S: Into<String>>(msg: S) -> Self {
        Self::Contract(msg.into())
    }

    /// Create a new GraphQL error
    pub fn graphql<S: Into<String>>(msg: S) -> Self {
        Self::GraphQL(msg.into())
    }

    /// Create a new configuration error
    pub fn config<S: Into<String>>(msg: S) -> Self {
        Self::Config(msg.into())
    }

    /// Create a new validation error
    pub fn validation<S: Into<String>>(msg: S) -> Self {
        Self::Validation(msg.into())
    }

    /// Create a new wallet error
    pub fn wallet<S: Into<String>>(msg: S) -> Self {
        Self::Wallet(msg.into())
    }

    /// Create a new provider error
    pub fn provider<S: Into<String>>(msg: S) -> Self {
        Self::Provider(msg.into())
    }

    /// Create a generic error with context
    pub fn other<S: Into<String>>(msg: S) -> Self {
        Self::Other(msg.into())
    }

    /// Create a new conversion error
    pub fn conversion<S: Into<String>>(msg: S) -> Self {
        Self::Other(format!("Conversion error: {}", msg.into()))
    }

    /// Create a new parsing error with a standardized message format.
    ///
    /// This constructor is used when there are issues parsing data, such as:
    /// - Converting between data formats (e.g., Python ABIs to JSON)
    /// - Parsing contract responses
    /// - Handling malformed input data
    ///
    /// The error message will be prefixed with "Parsing error: " for consistent error handling.
    pub fn parsing<S: Into<String>>(msg: S) -> Self {
        Self::Other(format!("Parsing error: {}", msg.into()))
    }
}