From 110fef1ce1a48824c30872be985f425a4dee3029 Mon Sep 17 00:00:00 2001 From: Julian Braha Date: Fri, 10 May 2024 16:57:16 +0100 Subject: [PATCH] Create SpacedropError enum for spacedrop() errors (#2469) core: spacedrop: use error enum --- core/src/api/p2p.rs | 4 ++-- core/src/p2p/operations/spacedrop.rs | 27 ++++++++++++++++++--------- crates/p2p/src/lib.rs | 15 +++++++++++++++ crates/p2p/src/peer.rs | 13 +------------ 4 files changed, 36 insertions(+), 23 deletions(-) diff --git a/core/src/api/p2p.rs b/core/src/api/p2p.rs index 89c88ea17..04a007671 100644 --- a/core/src/api/p2p.rs +++ b/core/src/api/p2p.rs @@ -153,8 +153,8 @@ pub(crate) fn mount() -> AlphaRouter { .collect::>(), ) .await - .map_err(|_err| { - rspc::Error::new(ErrorCode::InternalServerError, "todo: error".into()) + .map_err(|spacedrop_err| { + rspc::Error::new(ErrorCode::InternalServerError, spacedrop_err.to_string()) }) }) }) diff --git a/core/src/p2p/operations/spacedrop.rs b/core/src/p2p/operations/spacedrop.rs index 8c2d24a3a..0ddde18fa 100644 --- a/core/src/p2p/operations/spacedrop.rs +++ b/core/src/p2p/operations/spacedrop.rs @@ -12,6 +12,7 @@ use crate::p2p::{Header, P2PEvent, P2PManager}; use futures::future::join_all; use sd_p2p::{RemoteIdentity, UnicastStream}; use sd_p2p_block::{BlockSize, Range, SpaceblockRequest, SpaceblockRequests, Transfer}; +use thiserror::Error; use tokio::{ fs::{create_dir_all, File}, io::{AsyncReadExt, AsyncWriteExt, BufReader, BufWriter}, @@ -24,14 +25,25 @@ use uuid::Uuid; /// The amount of time to wait for a Spacedrop request to be accepted or rejected before it's automatically rejected pub(crate) const SPACEDROP_TIMEOUT: Duration = Duration::from_secs(60); -// TODO: Proper error handling +#[derive(Debug, Error)] +pub enum SpacedropError { + #[error("paths argument is an empty vector")] + EmptyPath, + #[error("error connecting to peer")] + FailedPeerConnection, + #[error("error creating stream: {0}")] + FailedNewStream(#[from] sd_p2p::NewStreamError), + #[error("error opening file: {0}")] + FailedFileOpen(#[from] std::io::Error), +} + pub async fn spacedrop( p2p: Arc, identity: RemoteIdentity, paths: Vec, -) -> Result { +) -> Result { if paths.is_empty() { - return Err(()); + return Err(SpacedropError::EmptyPath); } let (files, requests): (Vec<_>, Vec<_>) = join_all(paths.into_iter().map(|path| async move { @@ -55,10 +67,7 @@ pub async fn spacedrop( .await .into_iter() .collect::, std::io::Error>>() - .map_err(|err| { - warn!("error opening file: '{err:?}'"); - // TODO: Proper error type - })? + .map_err(|err| SpacedropError::FailedFileOpen(err))? .into_iter() .unzip(); @@ -72,13 +81,13 @@ pub async fn spacedrop( .get(&identity) .ok_or_else(|| { debug!("({id}): failed to find connection method with '{identity}'"); - // TODO: Proper error + SpacedropError::FailedPeerConnection })? .clone(); let mut stream = peer.new_stream().await.map_err(|err| { debug!("({id}): failed to connect to '{identity}': {err:?}"); - // TODO: Proper error + SpacedropError::FailedNewStream(err) })?; tokio::spawn(async move { diff --git a/crates/p2p/src/lib.rs b/crates/p2p/src/lib.rs index 3207e1066..48caf0eb7 100644 --- a/crates/p2p/src/lib.rs +++ b/crates/p2p/src/lib.rs @@ -20,3 +20,18 @@ pub use smart_guards::SmartWriteGuard; pub use stream::UnicastStream; pub use flume; + +use thiserror::Error; +use tokio::sync::{mpsc, oneshot}; + +#[derive(Debug, Error)] +pub enum NewStreamError { + #[error("No connection methods available for peer")] + NoConnectionMethodsAvailable, + #[error("The event loop is offline")] + EventLoopOffline(mpsc::error::SendError), + #[error("Failed to establish the connection w/ error: {0}")] + ConnectionNeverEstablished(oneshot::error::RecvError), + #[error("error connecting to peer: {0}")] + Connecting(String), +} diff --git a/crates/p2p/src/peer.rs b/crates/p2p/src/peer.rs index 9434b2147..ccbe84d3d 100644 --- a/crates/p2p/src/peer.rs +++ b/crates/p2p/src/peer.rs @@ -4,6 +4,7 @@ use std::{ sync::{Arc, PoisonError, RwLock, RwLockReadGuard, RwLockWriteGuard, Weak}, }; +use crate::NewStreamError; use thiserror::Error; use tokio::sync::{mpsc, oneshot}; use tracing::warn; @@ -298,15 +299,3 @@ impl Peer { } } } - -#[derive(Debug, Error)] -pub enum NewStreamError { - #[error("No connection methods available for peer")] - NoConnectionMethodsAvailable, - #[error("The event loop is offline")] - EventLoopOffline(mpsc::error::SendError), - #[error("Failed to establish the connection w/ error: {0}")] - ConnectionNeverEstablished(oneshot::error::RecvError), - #[error("error connecting to peer: {0}")] - Connecting(String), -}