mirror of
https://github.com/paradigmxyz/reth.git
synced 2026-01-30 09:38:24 -05:00
feature: better Address in use error message. (#6397)
This commit is contained in:
@@ -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()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<RethRpcModule>,
|
||||
},
|
||||
}
|
||||
|
||||
#[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()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user