1use thiserror::Error;
19
20#[derive(Debug, Error)]
22pub enum ApiError {
23 #[error("peer not found: {0}")]
24 PeerNotFound(String),
25 #[error("message verification failed: {reason}")]
26 VerificationFailed { reason: String },
27 #[error("rate limited: {peer_id}")]
28 RateLimited { peer_id: String },
29 #[error("replayed message from {peer_id} with nonce {nonce}")]
30 ReplayDetected { peer_id: String, nonce: u64 },
31 #[error("invalid schema: {reason}")]
32 InvalidSchema { reason: String },
33 #[error("serialization error: {0}")]
34 SerializationError(String),
35}
36pub type Result<T> = std::result::Result<T, ApiError>;
38
39#[cfg(test)]
40mod tests {
41 use super::*;
42 #[test]
43 fn all_variants_display() {
44 let es: Vec<ApiError> = vec![
45 ApiError::PeerNotFound("x".into()),
46 ApiError::VerificationFailed { reason: "x".into() },
47 ApiError::RateLimited {
48 peer_id: "x".into(),
49 },
50 ApiError::ReplayDetected {
51 peer_id: "x".into(),
52 nonce: 1,
53 },
54 ApiError::InvalidSchema { reason: "x".into() },
55 ApiError::SerializationError("x".into()),
56 ];
57 for e in &es {
58 assert!(!e.to_string().is_empty());
59 }
60 }
61 #[test]
62 fn result_alias() {
63 let ok: Result<u32> = Ok(1);
64 assert!(ok.is_ok());
65 }
66}