From 940e4b9e963ddd35c31cb0c2058f1617b8432c1c Mon Sep 17 00:00:00 2001 From: David Palm Date: Wed, 9 May 2018 06:33:09 +0200 Subject: [PATCH] Don't embed the io error in AddressParse --- util/network-devp2p/src/node_table.rs | 61 +++------------------------ util/network/src/error.rs | 8 ++-- 2 files changed, 9 insertions(+), 60 deletions(-) diff --git a/util/network-devp2p/src/node_table.rs b/util/network-devp2p/src/node_table.rs index f8251690df7..0ab72815f11 100644 --- a/util/network-devp2p/src/node_table.rs +++ b/util/network-devp2p/src/node_table.rs @@ -23,7 +23,6 @@ use serde_json; use std::collections::{HashMap, HashSet}; use std::fmt::{self, Display, Formatter}; use std::hash::{Hash, Hasher}; -use std::io; use std::net::{SocketAddr, ToSocketAddrs, SocketAddrV4, SocketAddrV6, Ipv4Addr, Ipv6Addr}; use std::path::PathBuf; use std::str::FromStr; @@ -124,16 +123,7 @@ impl FromStr for NodeEndpoint { udp_port: a.port() }), Ok(None) => bail!(ErrorKind::AddressResolve(None)), - Err(e) => { - match e.kind() { - io::ErrorKind::InvalidInput => { - Err(ErrorKind::AddressParse(Some(e)).into()) - }, - _ => { - Err(e.into()) - } - } - } + Err(_) => Err(ErrorKind::AddressParse.into()) // always an io::Error of InvalidInput kind } } } @@ -462,46 +452,21 @@ mod tests { fn endpoint_parse_empty_ip_string_returns_error() { let endpoint = NodeEndpoint::from_str(""); assert!(endpoint.is_err()); - assert_matches!( - endpoint.unwrap_err().kind(), - // TODO: after 1.27 is stable, remove the `&`s and `ref`s, see https://github.com/rust-lang/rust/pull/49394 - &ErrorKind::AddressParse(ref io_err) => { - assert!(io_err.is_some()); - if let &Some(ref e) = io_err { - assert_eq!(e.to_string(), "invalid socket address"); - } - } - ); + assert_matches!(endpoint.unwrap_err().kind(), &ErrorKind::AddressParse); } #[test] fn endpoint_parse_invalid_ip_string_returns_error() { let endpoint = NodeEndpoint::from_str("beef"); assert!(endpoint.is_err()); - assert_matches!( - endpoint.unwrap_err().kind(), - &ErrorKind::AddressParse(ref io_err) => { - assert!(io_err.is_some()); - if let &Some(ref e) = io_err { - assert_eq!(e.to_string(), "invalid socket address"); - } - } - ); + assert_matches!(endpoint.unwrap_err().kind(), &ErrorKind::AddressParse); } #[test] fn endpoint_parse_valid_ip_without_port_returns_error() { let endpoint = NodeEndpoint::from_str("123.123.123.123"); assert!(endpoint.is_err()); - assert_matches!( - endpoint.unwrap_err().kind(), - &ErrorKind::AddressParse(ref io_err) => { - assert!(io_err.is_some()); - if let &Some(ref e) = io_err { - assert_eq!(e.to_string(), "invalid socket address"); - } - } - ); + assert_matches!(endpoint.unwrap_err().kind(), &ErrorKind::AddressParse); let endpoint = NodeEndpoint::from_str("123.123.123.123:123"); assert!(endpoint.is_ok()) } @@ -526,25 +491,11 @@ mod tests { fn node_parse_fails_for_invalid_urls() { let node = Node::from_str("foo"); assert!(node.is_err()); - assert_matches!( - node.unwrap_err().kind(), - &ErrorKind::AddressParse(ref io_err) => { - if let &Some(ref e) = io_err { - assert_eq!(e.to_string(), "invalid socket address"); - } - } - ); + assert_matches!(node.unwrap_err().kind(), &ErrorKind::AddressParse); let node = Node::from_str("enode://foo@bar"); assert!(node.is_err()); - assert_matches!( - node.unwrap_err().kind(), - &ErrorKind::AddressParse(ref io_err) => { - if let &Some(ref e) = io_err { - assert_eq!(e.to_string(), "invalid port value"); - } - } - ); + assert_matches!(node.unwrap_err().kind(), &ErrorKind::AddressParse); } #[test] diff --git a/util/network/src/error.rs b/util/network/src/error.rs index 83d47958a1e..61044866966 100644 --- a/util/network/src/error.rs +++ b/util/network/src/error.rs @@ -89,9 +89,9 @@ error_chain! { errors { #[doc = "Error concerning the network address parsing subsystem."] - AddressParse(err: Option) { + AddressParse { description("Failed to parse network address"), - display("Failed to parse network address {}", err.as_ref().map_or("".to_string(), |e| e.to_string())), + display("Failed to parse network address"), } #[doc = "Error concerning the network address resolution subsystem."] @@ -169,9 +169,7 @@ impl From for Error { } impl From for Error { - fn from(_err: net::AddrParseError) -> Self { - ErrorKind::AddressParse(None).into() - } + fn from(_err: net::AddrParseError) -> Self { ErrorKind::AddressParse.into() } } #[test]