use thiserror::Error;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CollectErrorKind {
Warming,
SourceUnavailable,
Parse,
CounterReset,
Numeric,
IdentityFallback,
}
#[derive(Debug, Error)]
#[error("{kind}: {message}")]
pub struct CollectError {
pub kind: CollectErrorKind,
pub message: String,
#[source]
pub source: Option<Box<dyn std::error::Error + Send + Sync>>,
}
impl CollectError {
#[must_use]
pub fn new(kind: CollectErrorKind, message: impl Into<String>) -> Self {
Self {
kind,
message: message.into(),
source: None,
}
}
#[must_use]
pub fn with_source<E>(mut self, source: E) -> Self
where
E: std::error::Error + Send + Sync + 'static,
{
self.source = Some(Box::new(source));
self
}
#[must_use]
pub fn warming(message: impl Into<String>) -> Self {
Self::new(CollectErrorKind::Warming, message)
}
#[must_use]
pub fn counter_reset(message: impl Into<String>) -> Self {
Self::new(CollectErrorKind::CounterReset, message)
}
}
impl std::fmt::Display for CollectErrorKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let label = match self {
Self::Warming => "warming",
Self::SourceUnavailable => "source unavailable",
Self::Parse => "parse failure",
Self::CounterReset => "counter reset",
Self::Numeric => "numeric failure",
Self::IdentityFallback => "identity fallback",
};
f.write_str(label)
}
}