Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 71e626a

Browse files
author
Pascal Hertleif
committed
Improve verbose errors for socket addresses
Moves the point of adding error context to the net::addr module so that we have access to the raw address input and can include it in the error message.
1 parent aa7d1c2 commit 71e626a

File tree

5 files changed

+47
-13
lines changed

5 files changed

+47
-13
lines changed

‎src/net/addr.rs‎

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
use std::future::Future;
12
use std::mem;
23
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
34
use std::net::{SocketAddr, SocketAddrV4, SocketAddrV6};
45
use std::pin::Pin;
5-
use std::future::Future;
66

77
use crate::io;
88
use crate::task::{spawn_blocking, Context, JoinHandle, Poll};
9+
use crate::utils::Context as ErrorContext;
910

1011
cfg_not_docs! {
1112
macro_rules! ret {
@@ -67,6 +68,18 @@ pub enum ToSocketAddrsFuture<I> {
6768
Done,
6869
}
6970

71+
/// Wrap `std::io::Error` with additional message
72+
///
73+
/// Keeps the original error kind and stores the original I/O error as `source`.
74+
impl<T> ErrorContext for ToSocketAddrsFuture<T> {
75+
fn context(self, message: impl Fn() -> String) -> Self {
76+
match self {
77+
ToSocketAddrsFuture::Ready(res) => ToSocketAddrsFuture::Ready(res.context(message)),
78+
x => x,
79+
}
80+
}
81+
}
82+
7083
impl<I: Iterator<Item = SocketAddr>> Future for ToSocketAddrsFuture<I> {
7184
type Output = io::Result<I>;
7285

@@ -110,7 +123,9 @@ impl ToSocketAddrs for SocketAddrV4 {
110123
impl Future<Output = Self::Iter>,
111124
ToSocketAddrsFuture<Self::Iter>
112125
) {
113-
SocketAddr::V4(*self).to_socket_addrs()
126+
SocketAddr::V4(*self)
127+
.to_socket_addrs()
128+
.context(|| format!("could not resolve address `{}`", self))
114129
}
115130
}
116131

@@ -123,7 +138,9 @@ impl ToSocketAddrs for SocketAddrV6 {
123138
impl Future<Output = Self::Iter>,
124139
ToSocketAddrsFuture<Self::Iter>
125140
) {
126-
SocketAddr::V6(*self).to_socket_addrs()
141+
SocketAddr::V6(*self)
142+
.to_socket_addrs()
143+
.context(|| format!("could not resolve address `{}`", self))
127144
}
128145
}
129146

@@ -195,7 +212,9 @@ impl ToSocketAddrs for (&str, u16) {
195212

196213
let host = host.to_string();
197214
let task = spawn_blocking(move || {
198-
std::net::ToSocketAddrs::to_socket_addrs(&(host.as_str(), port))
215+
let addr = (host.as_str(), port);
216+
std::net::ToSocketAddrs::to_socket_addrs(&addr)
217+
.context(|| format!("could not resolve address `{:?}`", addr))
199218
});
200219
ToSocketAddrsFuture::Resolving(task)
201220
}
@@ -215,7 +234,10 @@ impl ToSocketAddrs for str {
215234
}
216235

217236
let addr = self.to_string();
218-
let task = spawn_blocking(move || std::net::ToSocketAddrs::to_socket_addrs(addr.as_str()));
237+
let task = spawn_blocking(move || {
238+
std::net::ToSocketAddrs::to_socket_addrs(addr.as_str())
239+
.context(|| format!("could not resolve address `{:?}`", addr))
240+
});
219241
ToSocketAddrsFuture::Resolving(task)
220242
}
221243
}

‎src/net/tcp/listener.rs‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use crate::net::driver::Watcher;
88
use crate::net::{TcpStream, ToSocketAddrs};
99
use crate::stream::Stream;
1010
use crate::task::{Context, Poll};
11-
use crate::utils::Context as _;
1211

1312
/// A TCP socket server, listening for connections.
1413
///
@@ -78,8 +77,7 @@ impl TcpListener {
7877
let mut last_err = None;
7978
let addrs = addrs
8079
.to_socket_addrs()
81-
.await
82-
.context(|| String::from("could not resolve addresses"))?;
80+
.await?;
8381

8482
for addr in addrs {
8583
match mio::net::TcpListener::bind(&addr) {

‎src/net/tcp/stream.rs‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ impl TcpStream {
7474
let mut last_err = None;
7575
let addrs = addrs
7676
.to_socket_addrs()
77-
.await
78-
.context(|| String::from("could not resolve addresses"))?;
77+
.await?;
7978

8079
for addr in addrs {
8180
let res = spawn_blocking(move || {

‎src/net/udp/mod.rs‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ impl UdpSocket {
7171
let mut last_err = None;
7272
let addrs = addrs
7373
.to_socket_addrs()
74-
.await
75-
.context(|| String::from("could not resolve addresses"))?;
74+
.await?;
7675

7776
for addr in addrs {
7877
match mio::net::UdpSocket::bind(&addr) {

‎tests/verbose_errors.rs‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use async_std::{fs, task};
1+
use async_std::{fs, net::ToSocketAddrs, task, io};
2+
use std::net::SocketAddr;
23

34
#[test]
45
fn open_file() {
@@ -14,3 +15,18 @@ fn open_file() {
1415
}
1516
})
1617
}
18+
19+
#[test]
20+
fn resolve_address() {
21+
task::block_on(async {
22+
let non_existing_addr = "ashjudlkahasdasdsikdhajik.asdasdasdasdasdasd.fjuiklashdbflasas:80";
23+
let res : Result<_, io::Error> = non_existing_addr.to_socket_addrs().await;
24+
match res {
25+
Ok(_) => panic!("Found address with random name: We live in a simulation"),
26+
Err(e) => assert_eq!(
27+
"could not resolve address `\"ashjudlkahasdasdsikdhajik.asdasdasdasdasdasd.fjuiklashdbflasas:80\"`",
28+
&format!("{}", e)
29+
),
30+
}
31+
})
32+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /