footing 0.1.13

Feature-gated foundations for Rust backend services
Documentation
//! Observability construction and initialization errors.

use std::fmt;

use thiserror::Error;

use crate::result::{ClassifyError, ErrorKind};

#[derive(Error)]
#[non_exhaustive]
/// Errors that can occur while handling observability.
pub enum ObservabilityError {
    #[error(
        "tracing service name must be non-empty and contain only ASCII letters, digits, '-', '_', '.', or ':'"
    )]
    /// The service name is invalid.
    InvalidServiceName,
    #[error("tracing env filter is invalid")]
    /// The filter is invalid.
    InvalidFilter(#[from] tracing_subscriber::filter::ParseError),
    #[error("tracing subscriber initialization failed")]
    /// Indicates init.
    Init(#[from] tracing_subscriber::util::TryInitError),
    #[error("local log timestamp offset could not be determined")]
    /// Indicates local time offset.
    LocalTimeOffset(#[source] time::error::IndeterminateOffset),
    #[error("trace field name is invalid")]
    /// The trace field name is invalid.
    InvalidTraceFieldName,
    #[error("event name is invalid")]
    /// The event name is invalid.
    InvalidEventName,
    #[error("health resource name is invalid")]
    /// The health name is invalid.
    InvalidHealthName,
}

impl fmt::Debug for ObservabilityError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("ObservabilityError")
            .field("code", &self.error_code())
            .finish_non_exhaustive()
    }
}

impl ClassifyError for ObservabilityError {
    fn error_kind(&self) -> ErrorKind {
        match self {
            Self::InvalidServiceName
            | Self::InvalidFilter(_)
            | Self::InvalidTraceFieldName
            | Self::InvalidEventName
            | Self::InvalidHealthName => ErrorKind::Validation,
            Self::LocalTimeOffset(_) => ErrorKind::Unavailable,
            Self::Init(_) => ErrorKind::Conflict,
        }
    }

    fn error_code(&self) -> crate::result::ErrorCode {
        crate::result::ErrorCode::internal(match self {
            Self::InvalidServiceName => "tracing_service_name_invalid",
            Self::InvalidFilter(_) => "tracing_filter_invalid",
            Self::Init(_) => "tracing_subscriber_init_failed",
            Self::LocalTimeOffset(_) => "tracing_local_time_offset_unavailable",
            Self::InvalidTraceFieldName => "trace_field_name_invalid",
            Self::InvalidEventName => "event_name_invalid",
            Self::InvalidHealthName => "health_name_invalid",
        })
    }
}