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    /// Generic error with custom message
93    #[error("Error: {message}")]
94    Other { message: String },
95}
96
97impl HeliaError {
98    /// Create a new custom error
99    pub fn other(message: impl Into<String>) -> Self {
100        Self::Other {
101            message: message.into(),
102        }
103    }
104
105    /// Create a new datastore error
106    pub fn datastore(message: impl Into<String>) -> Self {
107        Self::Datastore {
108            message: message.into(),
109        }
110    }
111
112    /// Create a new routing error
113    pub fn routing(message: impl Into<String>) -> Self {
114        Self::Routing {
115            message: message.into(),
116        }
117    }
118
119    /// Create a new network error
120    pub fn network(message: impl Into<String>) -> Self {
121        Self::Network {
122            message: message.into(),
123        }
124    }
125
126    /// Create a new invalid input error
127    pub fn invalid_input(message: impl Into<String>) -> Self {
128        Self::InvalidInput {
129            message: message.into(),
130        }
131    }
132}