tracing_better_stack/
error.rs

1use std::fmt;
2
3/// Error types that can occur when using the Better Stack tracing layer.
4///
5/// This enum represents all possible errors that can be returned by the library,
6/// including network errors, serialization issues, and configuration problems.
7#[derive(Debug)]
8pub enum BetterStackError {
9    /// HTTP request error when communicating with Better Stack.
10    ///
11    /// This includes network errors, timeouts, and connection issues.
12    HttpError(reqwest::Error),
13
14    /// Serialization error when formatting log events.
15    ///
16    /// This occurs when log data cannot be serialized to the configured format (JSON or MessagePack).
17    SerializationError(String),
18
19    /// Configuration error for invalid settings.
20    ///
21    /// This occurs when provided configuration values are invalid or incompatible.
22    ConfigError(String),
23
24    /// Runtime error for operational issues.
25    ///
26    /// This includes Better Stack API errors and other runtime failures.
27    RuntimeError(String),
28}
29
30impl fmt::Display for BetterStackError {
31    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32        match self {
33            Self::HttpError(e) => write!(f, "HTTP error: {}", e),
34            Self::SerializationError(msg) => write!(f, "Serialization error: {}", msg),
35            Self::ConfigError(msg) => write!(f, "Configuration error: {}", msg),
36            Self::RuntimeError(msg) => write!(f, "Runtime error: {}", msg),
37        }
38    }
39}
40
41impl std::error::Error for BetterStackError {}
42
43impl From<reqwest::Error> for BetterStackError {
44    fn from(error: reqwest::Error) -> Self {
45        Self::HttpError(error)
46    }
47}
48
49#[cfg(feature = "json")]
50impl From<serde_json::Error> for BetterStackError {
51    fn from(error: serde_json::Error) -> Self {
52        Self::SerializationError(error.to_string())
53    }
54}
55
56#[cfg(feature = "message_pack")]
57impl From<rmp_serde::encode::Error> for BetterStackError {
58    fn from(error: rmp_serde::encode::Error) -> Self {
59        Self::SerializationError(error.to_string())
60    }
61}
62
63#[cfg(feature = "message_pack")]
64impl From<rmp_serde::decode::Error> for BetterStackError {
65    fn from(error: rmp_serde::decode::Error) -> Self {
66        Self::SerializationError(error.to_string())
67    }
68}
69
70/// Convenience type alias for Results with BetterStackError.
71///
72/// This type alias simplifies function signatures throughout the library
73/// when returning results that may contain a `BetterStackError`.
74pub type Result<T> = std::result::Result<T, BetterStackError>;