footing 0.6.6

Feature-gated foundations for Rust backend services
Documentation
//! Log configuration and subscriber initialization errors.

use std::fmt;

use thiserror::Error;

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

/// Errors constructing log layers, filters, or the global subscriber.
#[derive(Error)]
#[non_exhaustive]
pub enum LoggingError {
    /// The service name is empty, oversized, or contains unsupported characters.
    #[error(
        "tracing service name must be non-empty and contain only ASCII letters, digits, '-', '_', '.', or ':'"
    )]
    InvalidServiceName,
    /// The tracing filter could not be parsed.
    #[error("tracing env filter is invalid")]
    InvalidFilter(#[from] tracing_subscriber::filter::ParseError),
    /// The global subscriber could not be installed.
    #[error("tracing subscriber initialization failed")]
    Init(#[from] tracing_subscriber::util::TryInitError),
    /// The local UTC offset could not be determined.
    #[error("local log timestamp offset could not be determined")]
    LocalTimeOffset(#[source] time::error::IndeterminateOffset),
}

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

impl ClassifyError for LoggingError {
    fn error_kind(&self) -> ErrorKind {
        match self {
            Self::InvalidServiceName | Self::InvalidFilter(_) => 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",
        })
    }
}