use thiserror::Error;
#[derive(Debug, Clone, Error)]
pub enum SharedInfraError {
#[error("channel send error for {target_domain}.{operation}: {cause}")]
ChannelSend {
target_domain: String,
operation: String,
cause: String,
},
#[error("channel receive error for {target_domain}.{operation}: {cause}")]
ChannelRecv {
target_domain: String,
operation: String,
cause: String,
},
#[error("oneshot reply timed out for {target_domain}.{operation} after {timeout}")]
OneshotReplyTimeout {
target_domain: String,
operation: String,
timeout: String,
},
#[error("oneshot reply channel closed for {target_domain}.{operation}: {cause}")]
OneshotReplyMissing {
target_domain: String,
operation: String,
cause: String,
},
}
#[cfg(test)]
mod tests {
use super::SharedInfraError;
#[test]
fn variants_display_exact_context_and_cause() {
let cases = [
(
SharedInfraError::ChannelSend {
target_domain: "orders".into(),
operation: "place".into(),
cause: "receiver dropped".into(),
},
"channel send error for orders.place: receiver dropped",
),
(
SharedInfraError::ChannelRecv {
target_domain: "history".into(),
operation: "load".into(),
cause: "channel closed".into(),
},
"channel receive error for history.load: channel closed",
),
(
SharedInfraError::OneshotReplyTimeout {
target_domain: "market".into(),
operation: "snapshot".into(),
timeout: "250ms".into(),
},
"oneshot reply timed out for market.snapshot after 250ms",
),
(
SharedInfraError::OneshotReplyMissing {
target_domain: "watch".into(),
operation: "refresh".into(),
cause: "reply sender dropped".into(),
},
"oneshot reply channel closed for watch.refresh: reply sender dropped",
),
];
for (error, expected) in cases {
assert_eq!(error.to_string(), expected);
}
}
}