1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
use std::io;
use crate::protocol;
use crate::server::socket_paths::client_socket_path;
/// Errors that can occur during client operation.
#[derive(Debug)]
pub enum ClientError {
/// Could not connect to the server's client socket.
ConnectionFailed(io::Error),
/// Server rejected our handshake.
HandshakeRejected { version: u32, error: String },
/// Server shut down.
ServerShutdown { reason: Option<String> },
/// Lost connection to the server.
ConnectionLost(io::Error),
/// Protocol error (framing, deserialization).
Protocol(protocol::FramingError),
}
impl std::fmt::Display for ClientError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ClientError::ConnectionFailed(err) => {
write!(f, "failed to connect to server: {err}")?;
let path = client_socket_path();
write!(
f,
"\nIs herdr server running? Start it with `herdr server`."
)?;
write!(f, "\nSocket path: {}", path.display())
}
ClientError::HandshakeRejected { version, error } => {
write!(f, "server rejected handshake (version {version}): {error}")
}
ClientError::ServerShutdown { reason } => {
match reason.as_deref() {
Some("detached") => {
if let Ok(reattach_command) =
std::env::var(crate::remote::REATTACH_COMMAND_ENV_VAR)
{
write!(f, "detached from remote server")?;
write!(f, "\nRun `{reattach_command}` to reattach")?;
} else {
write!(f, "detached from server")?;
write!(
f,
"\nRun `{}` to reattach",
crate::session::local_attach_command()
)?;
}
}
_ => {
write!(f, "server shut down")?;
if let Some(reason) = reason {
write!(f, ": {reason}")?;
}
}
}
Ok(())
}
ClientError::ConnectionLost(err) => {
if let Ok(reattach_command) = std::env::var(crate::remote::REATTACH_COMMAND_ENV_VAR)
{
write!(f, "lost connection to remote Herdr: {err}")?;
write!(f, "\nIf the remote server survived the SSH or network drop, its panes may still be running.")?;
write!(f, "\nRun `{reattach_command}` to reattach")
} else {
write!(f, "lost connection to server: {err}")
}
}
ClientError::Protocol(err) => write!(f, "protocol error: {err}"),
}
}
}
impl std::error::Error for ClientError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
ClientError::ConnectionFailed(err) => Some(err),
ClientError::ConnectionLost(err) => Some(err),
ClientError::Protocol(err) => Some(err),
_ => None,
}
}
}
impl From<protocol::FramingError> for ClientError {
fn from(err: protocol::FramingError) -> Self {
match err {
protocol::FramingError::UnexpectedEof => ClientError::ConnectionLost(io::Error::new(
io::ErrorKind::UnexpectedEof,
"server closed connection",
)),
protocol::FramingError::Io(err) => ClientError::ConnectionLost(err),
err => ClientError::Protocol(err),
}
}
}
|