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