helia_interface/
errors.rs

1//! Error types for Helia operations
2
3use thiserror::Error;
4
5/// Main error type for Helia operations
6#[derive(Error, Debug)]
7pub enum HeliaError {
8    /// Libp2p related errors (using string representation since libp2p::Error is complex)
9    #[error("Libp2p error: {0}")]
10    Libp2p(String),
11
12    /// CID parsing or validation errors
13    #[error("CID error: {0}")]
14    Cid(#[from] cid::Error),
15
16    /// Multihash errors
17    #[error("Multihash error: {0}")]
18    Multihash(#[from] multihash::Error),
19
20    /// Multiaddr parsing errors  
21    #[error("Multiaddr error: {0}")]
22    Multiaddr(String),
23
24    /// I/O errors
25    #[error("I/O error: {0}")]
26    Io(#[from] std::io::Error),
27
28    /// Serialization errors
29    #[error("Serialization error: {0}")]
30    Serialization(#[from] serde_json::Error),
31
32    /// Block not found
33    #[error("Block not found: {cid}")]
34    BlockNotFound { cid: cid::Cid },
35
36    /// Peer not found
37    #[error("Peer not found: {peer_id}")]
38    PeerNotFound { peer_id: libp2p::PeerId },
39
40    /// Timeout error
41    #[error("Operation timed out")]
42    Timeout,
43
44    /// Abort error
45    #[error("Operation was aborted")]
46    Aborted,
47
48    /// Invalid input
49    #[error("Invalid input: {message}")]
50    InvalidInput { message: String },
51
52    /// Node not started
53    #[error("Node not started")]
54    NodeNotStarted,
55
56    /// Node already started  
57    #[error("Node already started")]
58    NodeAlreadyStarted,
59
60    /// Codec not found
61    #[error("Codec not found: {code}")]
62    CodecNotFound { code: u64 },
63
64    /// Hasher not found
65    #[error("Hasher not found: {code}")]
66    HasherNotFound { code: u64 },
67
68    /// Pin not found
69    #[error("Pin not found: {cid}")]
70    PinNotFound { cid: cid::Cid },
71
72    /// Pin already exists
73    #[error("Pin already exists: {cid}")]
74    PinAlreadyExists { cid: cid::Cid },
75
76    /// Datastore error
77    #[error("Datastore error: {message}")]
78    Datastore { message: String },
79
80    /// Routing error
81    #[error("Routing error: {message}")]
82    Routing { message: String },
83
84    /// Network error
85    #[error("Network error: {message}")]
86    Network { message: String },
87
88    /// DNS resolution error
89    #[error("DNS error: {0}")]
90    Dns(#[from] trust_dns_resolver::error::ResolveError),
91
92    /// Not found error
93    #[error("Not found: {0}")]
94    NotFound(String),
95
96    /// Operation not supported
97    #[error("Operation not supported: {0}")]
98    OperationNotSupported(String),
99
100    /// Generic error with custom message
101    #[error("Error: {message}")]
102    Other { message: String },
103}
104
105impl HeliaError {
106    /// Create a new custom error
107    pub fn other(message: impl Into<String>) -> Self {
108        Self::Other {
109            message: message.into(),
110        }
111    }
112
113    /// Create a new datastore error
114    pub fn datastore(message: impl Into<String>) -> Self {
115        Self::Datastore {
116            message: message.into(),
117        }
118    }
119
120    /// Create a new routing error
121    pub fn routing(message: impl Into<String>) -> Self {
122        Self::Routing {
123            message: message.into(),
124        }
125    }
126
127    /// Create a new network error
128    pub fn network(message: impl Into<String>) -> Self {
129        Self::Network {
130            message: message.into(),
131        }
132    }
133
134    /// Create a new invalid input error
135    pub fn invalid_input(message: impl Into<String>) -> Self {
136        Self::InvalidInput {
137            message: message.into(),
138        }
139    }
140}