use thiserror::Error;
#[derive(Debug, Error, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum ChainSourceError {
#[error("chain source transport error: {0}")]
Transport(String),
#[error("malformed chain data: {0}")]
Malformed(String),
#[error("unsupported chain query: {0}")]
Unsupported(&'static str),
#[error("chain source request timed out")]
Timeout,
#[error("chain source rate limited the request")]
RateLimited,
#[error("no chain source provider available")]
NoProvider,
#[error("chain source returned {count} records, exceeding the {limit}-record cap")]
TooManyRecords {
count: usize,
limit: usize,
},
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn too_many_records_display_reports_count_and_limit() {
let err = ChainSourceError::TooManyRecords {
count: 100_001,
limit: 100_000,
};
assert_eq!(
err.to_string(),
"chain source returned 100001 records, exceeding the 100000-record cap"
);
}
#[test]
fn too_many_records_is_distinct_from_malformed() {
let too_many = ChainSourceError::TooManyRecords { count: 5, limit: 1 };
let malformed = ChainSourceError::Malformed("truncated coin record".to_string());
assert_ne!(too_many, malformed);
assert!(matches!(too_many, ChainSourceError::TooManyRecords { .. }));
assert!(!matches!(too_many, ChainSourceError::Malformed(_)));
}
}