indexbus_platform_obs/
errors.rs

1//! Error types and `Result` aliases.
2//!
3//! **Error semantics**
4//! - Errors are intended to be human-readable and suitable for logs/diagnostics.
5//! - Errors are not intended to be exhaustively pattern-matched for control flow.
6
7/// Result type used by this crate.
8pub type Result<T> = core::result::Result<T, Error>;
9use core::fmt;
10
11/// Errors produced by observability helpers.
12#[derive(Debug)]
13pub enum Error {
14    /// A simple, human-readable error message.
15    Message(String),
16
17    #[cfg(feature = "tracing")]
18    /// Tracing initialization failed.
19    TracingInit(String),
20
21    #[cfg(feature = "metrics")]
22    /// Metrics initialization failed.
23    MetricsInit(String),
24}
25
26impl Error {
27    /// Construct an [`crate::errors::Error::Message`].
28    pub fn msg(message: impl Into<String>) -> Self {
29        Self::Message(message.into())
30    }
31}
32
33impl fmt::Display for Error {
34    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35        match self {
36            Error::Message(message) => write!(f, "{message}"),
37            #[cfg(feature = "tracing")]
38            Error::TracingInit(message) => write!(f, "tracing init: {message}"),
39            #[cfg(feature = "metrics")]
40            Error::MetricsInit(message) => write!(f, "metrics init: {message}"),
41        }
42    }
43}
44
45#[cfg(feature = "std")]
46impl std::error::Error for Error {}