From 35ab1b15865e971c41c76d0a71d2901f58135164 Mon Sep 17 00:00:00 2001 From: Victor Shih Date: Mon, 5 Feb 2024 20:05:37 +0800 Subject: [PATCH] feature: better Address in use error message. (#6397) --- crates/net/network/src/error.rs | 30 ++++++++++++++++++++-- crates/rpc/rpc-builder/src/error.rs | 39 ++++++++++++++++++++++++++++- 2 files changed, 66 insertions(+), 3 deletions(-) diff --git a/crates/net/network/src/error.rs b/crates/net/network/src/error.rs index ae8b485cad..34fd22f3fa 100644 --- a/crates/net/network/src/error.rs +++ b/crates/net/network/src/error.rs @@ -9,7 +9,7 @@ use reth_eth_wire::{ use std::{fmt, io, io::ErrorKind, net::SocketAddr}; /// Service kind. -#[derive(Debug, PartialEq)] +#[derive(Debug, PartialEq, Copy, Clone)] pub enum ServiceKind { /// Listener service. Listener(SocketAddr), @@ -17,6 +17,16 @@ pub enum ServiceKind { Discovery(SocketAddr), } +impl ServiceKind { + /// Returns the appropriate flags for each variant. + pub fn flags(&self) -> &'static str { + match self { + ServiceKind::Listener(_) => "--port", + ServiceKind::Discovery(_) => "--discovery.port", + } + } +} + impl fmt::Display for ServiceKind { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { @@ -33,7 +43,7 @@ pub enum NetworkError { #[error(transparent)] Io(#[from] io::Error), /// Error when an address is already in use. - #[error("address {kind} is already in use (os error 98)")] + #[error("address {kind} is already in use (os error 98). Choose a different port using {}", kind.flags())] AddressAlreadyInUse { /// Service kind. kind: ServiceKind, @@ -269,6 +279,7 @@ impl SessionError for io::Error { #[cfg(test)] mod tests { use super::*; + use std::net::{Ipv4Addr, SocketAddrV4}; #[test] fn test_is_fatal_disconnect() { @@ -295,4 +306,19 @@ mod tests { )); assert_eq!(err.should_backoff(), Some(BackoffKind::Low)); } + + #[test] + fn test_address_in_use_message() { + let addr = SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 1234)); + let kinds = [ServiceKind::Discovery(addr), ServiceKind::Listener(addr)]; + + for kind in &kinds { + let err = NetworkError::AddressAlreadyInUse { + kind: *kind, + error: io::Error::from(ErrorKind::AddrInUse), + }; + + assert!(err.to_string().contains(kind.flags())); + } + } } diff --git a/crates/rpc/rpc-builder/src/error.rs b/crates/rpc/rpc-builder/src/error.rs index 5b13e78824..a1d6994f89 100644 --- a/crates/rpc/rpc-builder/src/error.rs +++ b/crates/rpc/rpc-builder/src/error.rs @@ -17,6 +17,18 @@ pub enum ServerKind { Auth(SocketAddr), } +impl ServerKind { + /// Returns the appropriate flags for each variant. + pub fn flags(&self) -> &'static str { + match self { + ServerKind::Http(_) => "--http.port", + ServerKind::WS(_) => "--ws.port", + ServerKind::WsHttp(_) => "--ws.port and --http.port", + ServerKind::Auth(_) => "--authrpc.port", + } + } +} + impl std::fmt::Display for ServerKind { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { @@ -35,7 +47,7 @@ pub enum RpcError { #[error(transparent)] RpcError(#[from] JsonRpseeError), /// Address already in use. - #[error("address {kind} is already in use (os error 98)")] + #[error("address {kind} is already in use (os error 98). Choose a different port using {}", kind.flags())] AddressAlreadyInUse { /// Server kind. kind: ServerKind, @@ -96,3 +108,28 @@ pub enum WsHttpSamePortError { ws_modules: Vec, }, } + +#[cfg(test)] +mod tests { + use super::*; + use std::net::{Ipv4Addr, SocketAddrV4}; + #[test] + fn test_address_in_use_message() { + let addr = SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 1234)); + let kinds = [ + ServerKind::Http(addr), + ServerKind::WS(addr), + ServerKind::WsHttp(addr), + ServerKind::Auth(addr), + ]; + + for kind in &kinds { + let err = RpcError::AddressAlreadyInUse { + kind: *kind, + error: io::Error::from(ErrorKind::AddrInUse), + }; + + assert!(err.to_string().contains(kind.flags())); + } + } +}