cuprate_p2p_core/
error.rs

1use std::sync::{Arc, OnceLock};
2
3pub struct SharedError<T>(Arc<OnceLock<T>>);
4
5impl<T> Clone for SharedError<T> {
6    fn clone(&self) -> Self {
7        Self(Arc::clone(&self.0))
8    }
9}
10
11impl<T> Default for SharedError<T> {
12    fn default() -> Self {
13        Self::new()
14    }
15}
16
17impl<T> SharedError<T> {
18    pub fn new() -> Self {
19        Self(Arc::new(OnceLock::new()))
20    }
21
22    pub fn try_get_err(&self) -> Option<&T> {
23        self.0.get()
24    }
25
26    pub fn try_insert_err(&self, err: T) -> Result<(), &T> {
27        self.0.set(err).map_err(|_| self.0.get().unwrap())
28    }
29}
30
31#[derive(Debug, thiserror::Error)]
32pub enum PeerError {
33    #[error("The connection timed out.")]
34    TimedOut,
35    #[error("The connection was closed.")]
36    ConnectionClosed,
37    #[error("The connection tasks client channel was closed")]
38    ClientChannelClosed,
39    #[error("error with peer response: {0}")]
40    ResponseError(&'static str),
41    #[error("the peer sent an incorrect response to our request")]
42    PeerSentIncorrectResponse,
43    #[error("the peer sent an invalid message")]
44    PeerSentInvalidMessage,
45    #[error("inner service error: {0}")]
46    ServiceError(#[from] tower::BoxError),
47    #[error("bucket error: {0}")]
48    BucketError(#[from] cuprate_wire::BucketError),
49    #[error("handshake error: {0}")]
50    Handshake(#[from] crate::client::HandshakeError),
51    #[error("i/o error: {0}")]
52    IO(#[from] std::io::Error),
53}