diff --git a/CHANGELOG.md b/CHANGELOG.md index c2087cacb..e79aa699d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,8 @@ ### Changed +* pool: update `Error::WebSocket` variant inner type ([Yuki Kishimoto]) + ### Added * pool: add `Relay::try_connect` ([Yuki Kishimoto]) diff --git a/crates/nostr-relay-pool/src/relay/error.rs b/crates/nostr-relay-pool/src/relay/error.rs index 6f030c460..a9ba57b2b 100644 --- a/crates/nostr-relay-pool/src/relay/error.rs +++ b/crates/nostr-relay-pool/src/relay/error.rs @@ -5,6 +5,7 @@ use std::fmt; use std::time::Duration; +use async_wsocket::Error as WsError; use nostr::event; use nostr::event::builder; use nostr::message::MessageHandleError; @@ -18,7 +19,7 @@ use crate::RelayPoolNotification; #[derive(Debug)] pub enum Error { /// WebSocket error - WebSocket(Box), + WebSocket(WsError), /// Shared state error SharedState(SharedStateError), /// MessageHandle error @@ -180,13 +181,9 @@ impl fmt::Display for Error { } } -impl Error { - #[inline] - pub(super) fn websocket(error: E) -> Self - where - E: std::error::Error + Send + Sync + 'static, - { - Self::WebSocket(Box::new(error)) +impl From for Error { + fn from(e: WsError) -> Self { + Self::WebSocket(e) } } diff --git a/crates/nostr-relay-pool/src/relay/inner.rs b/crates/nostr-relay-pool/src/relay/inner.rs index 36b52d55b..ed1f81d2d 100644 --- a/crates/nostr-relay-pool/src/relay/inner.rs +++ b/crates/nostr-relay-pool/src/relay/inner.rs @@ -455,10 +455,7 @@ impl InnerRelay { } // Try to connect - let stream = self - ._try_connect(timeout, false) - .await - .map_err(Error::websocket)?; + let stream = self._try_connect(timeout, false).await?; // Spawn connection task self.spawn_and_try_connect(Some(stream)); @@ -800,7 +797,7 @@ impl InnerRelay { let _ping = ping; while let Some(msg) = ws_rx.next().await { - match msg.map_err(Error::websocket)? { + match msg? { #[cfg(not(target_arch = "wasm32"))] WsMessage::Pong(bytes) => { if self.flags.has_ping() { @@ -2398,7 +2395,7 @@ impl InnerRelay { async fn send_ws_msgs(tx: &mut Sink, msgs: Vec) -> Result<(), Error> { let mut stream = futures_util::stream::iter(msgs.into_iter().map(Ok)); match time::timeout(Some(WEBSOCKET_TX_TIMEOUT), tx.send_all(&mut stream)).await { - Some(res) => res.map_err(Error::websocket), + Some(res) => Ok(res?), None => Err(Error::Timeout), } } @@ -2406,7 +2403,7 @@ async fn send_ws_msgs(tx: &mut Sink, msgs: Vec) -> Result<(), Error> /// Send WebSocket messages with timeout set to [WEBSOCKET_TX_TIMEOUT]. async fn close_ws(tx: &mut Sink) -> Result<(), Error> { match time::timeout(Some(WEBSOCKET_TX_TIMEOUT), tx.close()).await { - Some(res) => res.map_err(Error::websocket), + Some(res) => Ok(res?), None => Err(Error::Timeout), } }