fix(daemon): Add context to error when unable to connect (#2394)

Recently, it seems, the socket location for the daemon moved and this
caused me to scratch my head briefly since I saw errors from the client
connecting to the daemon, but the daemon was clearly running and the
socket seemed to exist.

This patch includes more context when the client fails to connect to the
daemon. The path is included to help the user understand where the
client was looking, and `wrap_err_with()` is used to show the user the
cause of the error. This changes the error message from:

Error: failed to connect to local atuin daemon. Is it running?

to:

Error: failed to connect to local atuin daemon at /run/user/1001/atuin.sock. Is it running?

Caused by:
   0: transport error
   1: No such file or directory (os error 2)
   2: No such file or directory (os error 2)
This commit is contained in:
Jeremy Cline 2024-10-02 20:44:22 -04:00 committed by GitHub
parent a1a157cc97
commit 80e950c648
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,4 +1,4 @@
use eyre::{eyre, Result};
use eyre::{Context, Result};
#[cfg(windows)]
use tokio::net::TcpStream;
use tonic::transport::{Channel, Endpoint, Uri};
@ -23,6 +23,7 @@ pub struct HistoryClient {
impl HistoryClient {
#[cfg(unix)]
pub async fn new(path: String) -> Result<Self> {
let log_path = path.clone();
let channel = Endpoint::try_from("http://atuin_local_daemon:0")?
.connect_with_connector(service_fn(move |_: Uri| {
let path = path.clone();
@ -32,7 +33,12 @@ impl HistoryClient {
}
}))
.await
.map_err(|_| eyre!("failed to connect to local atuin daemon. Is it running?"))?;
.wrap_err_with(|| {
format!(
"failed to connect to local atuin daemon at {}. Is it running?",
&log_path
)
})?;
let client = HistoryServiceClient::new(channel);
@ -50,7 +56,12 @@ impl HistoryClient {
}
}))
.await
.map_err(|_| eyre!("failed to connect to local atuin daemon. Is it running?"))?;
.wrap_err_with(|| {
format!(
"failed to connect to local atuin daemon at 127.0.0.1:{}. Is it running?",
port
)
})?;
let client = HistoryServiceClient::new(channel);