Skip to content

Commit

Permalink
improve error messages for socket bind and connect errors
Browse files Browse the repository at this point in the history
  • Loading branch information
pr2502 committed Jul 6, 2024
1 parent 3198a79 commit dc96d7e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
4 changes: 3 additions & 1 deletion src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ pub async fn run(config: &Config, server: String, args: Vec<String>) -> Result<(
}
}

let mut stream = Stream::connect(&config.connect).await.context("connect")?;
let mut stream = Stream::connect(&config.connect)
.await
.context("connecting to server")?;
let mut stdio = BufStream::new(io::join(io::stdin(), io::stdout()));

// Wait for the client to send `initialize` request.
Expand Down
1 change: 1 addition & 0 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub async fn run(config: &Config) -> Result<()> {
let next_client_id = || next_client_id.fetch_add(1, Ordering::Relaxed);

let listener = Listener::bind(&config.listen).await.context("listen")?;
info!(socket = ?config.listen, "listening");
loop {
match listener.accept().await {
Ok((socket, _addr)) => {
Expand Down
35 changes: 22 additions & 13 deletions src/socketwrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::pin::Pin;
use std::task::{Context, Poll};
use std::{io, net};

use anyhow::{Context as _, Result};
use pin_project_lite::pin_project;
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
use tokio::net::{tcp, TcpListener, TcpStream};
Expand Down Expand Up @@ -136,15 +137,17 @@ pin_project! {
}

impl Stream {
pub async fn connect(addr: &Address) -> io::Result<Stream> {
pub async fn connect(addr: &Address) -> Result<Stream> {
match addr {
Address::Tcp(ip_addr, port) => Ok(Stream::Tcp {
tcp: TcpStream::connect((*ip_addr, *port)).await?,
}),
Address::Tcp(ip_addr, port) => TcpStream::connect((*ip_addr, *port))
.await
.with_context(|| format!("connecting to tcp socket {ip_addr}:{port}"))
.map(|tcp| Stream::Tcp { tcp }),
#[cfg(target_family = "unix")]
Address::Unix(path) => Ok(Stream::Unix {
unix: UnixStream::connect(path).await?,
}),
Address::Unix(path) => UnixStream::connect(path)
.await
.with_context(|| format!("connecting to unix socket {path:?}"))
.map(|unix| Stream::Unix { unix }),
}
}

Expand Down Expand Up @@ -232,19 +235,25 @@ pub enum Listener {
}

impl Listener {
pub async fn bind(addr: &Address) -> io::Result<Listener> {
pub async fn bind(addr: &Address) -> Result<Listener> {
match addr {
Address::Tcp(ip_addr, port) => {
Ok(Listener::Tcp(TcpListener::bind((*ip_addr, *port)).await?))
}
Address::Tcp(ip_addr, port) => TcpListener::bind((*ip_addr, *port))
.await
.with_context(|| format!("binding to tcp socket {ip_addr}:{port}"))
.map(Listener::Tcp),
#[cfg(target_family = "unix")]
Address::Unix(path) => {
match fs::remove_file(path) {
Ok(()) => (),
Err(e) if e.kind() == io::ErrorKind::NotFound => (),
Err(e) => return Err(e),
Err(e) => {
return Err(e)
.with_context(|| format!("removing old unix socket file {path:?}"))
}
}
Ok(Listener::Unix(UnixListener::bind(path)?))
UnixListener::bind(path)
.with_context(|| format!("binding to unix socket {path:?}"))
.map(Listener::Unix)
}
}
}
Expand Down

0 comments on commit dc96d7e

Please sign in to comment.