cuprate-hinto-janai/p2p/p2p-core/src/error.rs
hinto-janai 848a6a71c4
Some checks failed
Audit / audit (push) Has been cancelled
CI / fmt (push) Has been cancelled
CI / typo (push) Has been cancelled
CI / ci (macos-latest, stable, bash) (push) Has been cancelled
CI / ci (ubuntu-latest, stable, bash) (push) Has been cancelled
CI / ci (windows-latest, stable-x86_64-pc-windows-gnu, msys2 {0}) (push) Has been cancelled
Deny / audit (push) Has been cancelled
Doc / build (push) Has been cancelled
Doc / deploy (push) Has been cancelled
p2p/p2p-core: enable workspace lints (#288)
* p2p-core: enable workspace lints

* fmt

* fix tests

* fixes

* fixes

* fixes

* expect reason
2024-09-21 01:37:06 +01:00

53 lines
1.4 KiB
Rust

use std::sync::{Arc, OnceLock};
pub struct SharedError<T>(Arc<OnceLock<T>>);
impl<T> Clone for SharedError<T> {
fn clone(&self) -> Self {
Self(Arc::clone(&self.0))
}
}
impl<T> Default for SharedError<T> {
fn default() -> Self {
Self::new()
}
}
impl<T> SharedError<T> {
pub fn new() -> Self {
Self(Arc::new(OnceLock::new()))
}
pub fn try_get_err(&self) -> Option<&T> {
self.0.get()
}
pub fn try_insert_err(&self, err: T) -> Result<(), &T> {
self.0.set(err).map_err(|_| self.0.get().unwrap())
}
}
#[derive(Debug, thiserror::Error)]
pub enum PeerError {
#[error("The connection timed out.")]
TimedOut,
#[error("The connection was closed.")]
ConnectionClosed,
#[error("The connection tasks client channel was closed")]
ClientChannelClosed,
#[error("error with peer response: {0}")]
ResponseError(&'static str),
#[error("the peer sent an incorrect response to our request")]
PeerSentIncorrectResponse,
#[error("the peer sent an invalid message")]
PeerSentInvalidMessage,
#[error("inner service error: {0}")]
ServiceError(#[from] tower::BoxError),
#[error("bucket error: {0}")]
BucketError(#[from] cuprate_wire::BucketError),
#[error("handshake error: {0}")]
Handshake(#[from] crate::client::HandshakeError),
#[error("i/o error: {0}")]
IO(#[from] std::io::Error),
}