use std::fmt;
use thiserror::Error;
use crate::result::{ClassifyError, ErrorKind};
#[derive(Error)]
#[non_exhaustive]
pub enum ObservabilityError {
#[error(
"tracing service name must be non-empty and contain only ASCII letters, digits, '-', '_', '.', or ':'"
)]
InvalidServiceName,
#[error("tracing env filter is invalid")]
InvalidFilter(#[from] tracing_subscriber::filter::ParseError),
#[error("tracing subscriber initialization failed")]
Init(#[from] tracing_subscriber::util::TryInitError),
#[error("local log timestamp offset could not be determined")]
LocalTimeOffset(#[source] time::error::IndeterminateOffset),
#[error("trace field name is invalid")]
InvalidTraceFieldName,
#[error("event name is invalid")]
InvalidEventName,
#[error("health resource 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",
})
}
}