Skip to main content

hyprstream_p2p/
error.rs

1//! Error handling for GitTorrent
2
3/// Result type alias for GitTorrent operations
4pub type Result<T> = std::result::Result<T, Error>;
5
6/// Main error type for GitTorrent operations
7#[derive(Debug, thiserror::Error)]
8pub enum Error {
9    #[error("IO error: {0}")]
10    Io(#[from] std::io::Error),
11
12    #[error("JSON serialization error: {0}")]
13    Json(#[from] serde_json::Error),
14
15    #[error("Configuration error: {0}")]
16    Config(#[from] config::ConfigError),
17
18    #[error("Git error: {0}")]
19    Git(#[from] git2::Error),
20
21    #[error("Hex decoding error: {0}")]
22    Hex(#[from] hex::FromHexError),
23
24    #[error("UTF-8 conversion error: {0}")]
25    Utf8(#[from] std::string::FromUtf8Error),
26
27    #[error("UTF-8 string error: {0}")]
28    Utf8Str(#[from] std::str::Utf8Error),
29
30    #[error("System time error: {0}")]
31    SystemTime(#[from] std::time::SystemTimeError),
32
33    #[error("Serialization error: {0}")]
34    Bincode(#[from] Box<bincode::ErrorKind>),
35
36    #[error("Invalid SHA256: {0}")]
37    InvalidSha256(String),
38
39    #[error("Invalid git hash (expected 40 or 64 hex chars): {0}")]
40    InvalidHash(String),
41
42    #[error("Invalid mutable key: {0}")]
43    InvalidMutableKey(String),
44
45    #[error("Invalid URL: {0}")]
46    InvalidUrl(String),
47
48    #[error("Crypto error: {0}")]
49    Crypto(String),
50
51    #[error("DHT error: {0}")]
52    Dht(String),
53
54    #[error("Protocol error: {0}")]
55    Protocol(String),
56
57    #[error("Timeout error: {0}")]
58    Timeout(String),
59
60    #[error("Not found: {0}")]
61    NotFound(String),
62
63    #[error("Other error: {0}")]
64    Other(String),
65}
66
67impl From<tokio::sync::oneshot::error::RecvError> for Error {
68    fn from(err: tokio::sync::oneshot::error::RecvError) -> Self {
69        Error::Other(format!("Channel receive error: {err}"))
70    }
71}
72
73impl From<std::convert::Infallible> for Error {
74    fn from(_: std::convert::Infallible) -> Self {
75        // Infallible can never actually occur, so this should never be called
76        unreachable!("Infallible error should never occur")
77    }
78}
79
80impl<T> From<tokio::sync::mpsc::error::SendError<T>> for Error {
81    fn from(err: tokio::sync::mpsc::error::SendError<T>) -> Self {
82        Error::Other(format!("Channel send error: {err}"))
83    }
84}
85
86impl Error {
87    /// Create a crypto error
88    pub fn crypto<S: Into<String>>(msg: S) -> Self {
89        Error::Crypto(msg.into())
90    }
91
92    /// Create a DHT error
93    pub fn dht<S: Into<String>>(msg: S) -> Self {
94        Error::Dht(msg.into())
95    }
96
97    /// Create a protocol error
98    pub fn protocol<S: Into<String>>(msg: S) -> Self {
99        Error::Protocol(msg.into())
100    }
101
102    /// Create a timeout error
103    pub fn timeout<S: Into<String>>(msg: S) -> Self {
104        Error::Timeout(msg.into())
105    }
106
107    /// Create a not found error
108    pub fn not_found<S: Into<String>>(msg: S) -> Self {
109        Error::NotFound(msg.into())
110    }
111
112    /// Create a generic error
113    pub fn other<S: Into<String>>(msg: S) -> Self {
114        Error::Other(msg.into())
115    }
116}