tracing-better-stack 0.1.0

A tracing-subscriber layer for Better Stack (Logtail) logging
Documentation
use std::fmt;

/// Error types that can occur when using the Better Stack tracing layer.
///
/// This enum represents all possible errors that can be returned by the library,
/// including network errors, serialization issues, and configuration problems.
#[derive(Debug)]
pub enum BetterStackError {
    /// HTTP request error when communicating with Better Stack.
    ///
    /// This includes network errors, timeouts, and connection issues.
    HttpError(reqwest::Error),

    /// Serialization error when formatting log events.
    ///
    /// This occurs when log data cannot be serialized to the configured format (JSON or MessagePack).
    SerializationError(String),

    /// Configuration error for invalid settings.
    ///
    /// This occurs when provided configuration values are invalid or incompatible.
    ConfigError(String),

    /// Runtime error for operational issues.
    ///
    /// This includes Better Stack API errors and other runtime failures.
    RuntimeError(String),
}

impl fmt::Display for BetterStackError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::HttpError(e) => write!(f, "HTTP error: {}", e),
            Self::SerializationError(msg) => write!(f, "Serialization error: {}", msg),
            Self::ConfigError(msg) => write!(f, "Configuration error: {}", msg),
            Self::RuntimeError(msg) => write!(f, "Runtime error: {}", msg),
        }
    }
}

impl std::error::Error for BetterStackError {}

impl From<reqwest::Error> for BetterStackError {
    fn from(error: reqwest::Error) -> Self {
        Self::HttpError(error)
    }
}

#[cfg(feature = "json")]
impl From<serde_json::Error> for BetterStackError {
    fn from(error: serde_json::Error) -> Self {
        Self::SerializationError(error.to_string())
    }
}

#[cfg(feature = "message_pack")]
impl From<rmp_serde::encode::Error> for BetterStackError {
    fn from(error: rmp_serde::encode::Error) -> Self {
        Self::SerializationError(error.to_string())
    }
}

#[cfg(feature = "message_pack")]
impl From<rmp_serde::decode::Error> for BetterStackError {
    fn from(error: rmp_serde::decode::Error) -> Self {
        Self::SerializationError(error.to_string())
    }
}

/// Convenience type alias for Results with BetterStackError.
///
/// This type alias simplifies function signatures throughout the library
/// when returning results that may contain a `BetterStackError`.
pub type Result<T> = std::result::Result<T, BetterStackError>;