use std::{
error::Error,
fmt::{self, Display, Formatter},
};
use crate::{catalog::Code, wire::WireLimitError};
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum HealthBuildError {
DiagnosticNotRegistered {
diagnostic: &'static str,
},
ValidatorMissing {
code: Code,
},
}
impl Display for HealthBuildError {
fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
match self {
Self::DiagnosticNotRegistered { diagnostic } => {
write!(formatter, "health finding {diagnostic} is not registered")
}
Self::ValidatorMissing { code } => {
write!(
formatter,
"health finding {code} has no compiled evidence validator"
)
}
}
}
}
impl Error for HealthBuildError {}
#[derive(Debug)]
#[non_exhaustive]
pub enum HealthEncodeError {
EvidenceSerialization(serde_json::Error),
EvidenceNotObject,
EvidenceSchemaMismatch {
path: String,
reason: String,
},
WireLimit(WireLimitError),
BodySerialization(serde_json::Error),
}
impl Display for HealthEncodeError {
fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
match self {
Self::EvidenceSerialization(error) => write!(formatter, "serialize evidence: {error}"),
Self::EvidenceNotObject => formatter.write_str("evidence must serialize as an object"),
Self::EvidenceSchemaMismatch { path, reason } => {
write!(
formatter,
"evidence violates its schema at {path}: {reason}"
)
}
Self::WireLimit(error) => Display::fmt(error, formatter),
Self::BodySerialization(error) => {
write!(formatter, "serialize health finding body: {error}")
}
}
}
}
impl Error for HealthEncodeError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
Self::EvidenceSerialization(error) | Self::BodySerialization(error) => Some(error),
Self::WireLimit(error) => Some(error),
Self::EvidenceNotObject | Self::EvidenceSchemaMismatch { .. } => None,
}
}
}