Skip to content

Commit a32e168

Browse files
committed
Refactor Error using thiserror::Error derive macro
Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
1 parent 39bbb5c commit a32e168

File tree

1 file changed

+18
-59
lines changed

1 file changed

+18
-59
lines changed

src/error.rs

Lines changed: 18 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,38 @@
1-
use std::fmt;
21
use std::io;
32

43
/// Errors that occur when interacting with a remote process.
5-
#[derive(Debug)]
4+
#[derive(Debug, thiserror::Error)]
65
#[non_exhaustive]
76
pub enum Error {
87
/// The master connection failed.
9-
Master(io::Error),
8+
#[error("the master connection failed")]
9+
Master(#[source] io::Error),
1010

1111
/// Failed to establish initial connection to the remote host.
12-
Connect(io::Error),
12+
#[error("failed to connect to the remote host")]
13+
Connect(#[source] io::Error),
1314

1415
/// Failed to run the `ssh` command locally.
1516
#[cfg(feature = "process-mux")]
1617
#[cfg_attr(docsrs, doc(cfg(feature = "process-mux")))]
17-
Ssh(io::Error),
18+
#[error("the local ssh command could not be executed")]
19+
Ssh(#[source] io::Error),
1820

1921
/// Failed to connect to the ssh multiplex server.
2022
#[cfg(feature = "native-mux")]
2123
#[cfg_attr(docsrs, doc(cfg(feature = "native-mux")))]
22-
SshMux(openssh_mux_client::Error),
24+
#[error("failed to connect to the ssh multiplex server")]
25+
SshMux(#[source] openssh_mux_client::Error),
2326

2427
/// Invalid command that contains null byte.
2528
#[cfg(feature = "native-mux")]
2629
#[cfg_attr(docsrs, doc(cfg(feature = "native-mux")))]
30+
#[error("invalid command: Command contains null byte.")]
2731
InvalidCommand,
2832

2933
/// The remote process failed.
30-
Remote(io::Error),
34+
#[error("the remote command could not be executed")]
35+
Remote(#[source] io::Error),
3136

3237
/// The connection to the remote host was severed.
3338
///
@@ -36,6 +41,7 @@ pub enum Error {
3641
///
3742
/// You should call [`Session::check`](crate::Session::check) to verify if you get
3843
/// this error back.
44+
#[error("the connection was terminated")]
3945
Disconnected,
4046

4147
/// Remote process is terminated.
@@ -51,13 +57,16 @@ pub enum Error {
5157
/// instead of `Disconnect`ed.
5258
///
5359
/// It is thus recommended to create your own workaround for your particular use cases.
60+
#[error("the remote process has terminated")]
5461
RemoteProcessTerminated,
5562

5663
/// Failed to remove temporary dir where ssh socket and output is stored.
57-
Cleanup(io::Error),
64+
#[error("failed to remove temporary ssh session directory")]
65+
Cleanup(#[source] io::Error),
5866

5967
/// IO Error when creating/reading/writing from ChildStdin, ChildStdout, ChildStderr.
60-
ChildIo(io::Error),
68+
#[error("failure while accessing standard i/o of remote process")]
69+
ChildIo(#[source] io::Error),
6170
}
6271

6372
#[cfg(feature = "native-mux")]
@@ -83,56 +92,6 @@ impl From<openssh_mux_client::Error> for Error {
8392
}
8493
}
8594

86-
impl fmt::Display for Error {
87-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
88-
match *self {
89-
Error::Master(_) => write!(f, "the master connection failed"),
90-
Error::Connect(_) => write!(f, "failed to connect to the remote host"),
91-
92-
#[cfg(feature = "process-mux")]
93-
Error::Ssh(_) => write!(f, "the local ssh command could not be executed"),
94-
95-
Error::Remote(_) => write!(f, "the remote command could not be executed"),
96-
Error::Disconnected => write!(f, "the connection was terminated"),
97-
Error::Cleanup(_) => write!(f, "failed to remove temporary ssh session directory"),
98-
Error::ChildIo(_) => {
99-
write!(f, "failure while accessing standard I/O of remote process")
100-
}
101-
102-
Error::RemoteProcessTerminated => write!(f, "the remote process has terminated"),
103-
104-
#[cfg(feature = "native-mux")]
105-
Error::SshMux(_) => write!(f, "failed to connect to the ssh multiplex server"),
106-
107-
#[cfg(feature = "native-mux")]
108-
Error::InvalidCommand => write!(f, "invalid command: Command contains null byte."),
109-
}
110-
}
111-
}
112-
113-
impl std::error::Error for Error {
114-
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
115-
match *self {
116-
Error::Master(ref e)
117-
| Error::Connect(ref e)
118-
| Error::Remote(ref e)
119-
| Error::Cleanup(ref e)
120-
| Error::ChildIo(ref e) => Some(e),
121-
122-
Error::RemoteProcessTerminated | Error::Disconnected => None,
123-
124-
#[cfg(feature = "native-mux")]
125-
Error::InvalidCommand => None,
126-
127-
#[cfg(feature = "process-mux")]
128-
Error::Ssh(ref e) => Some(e),
129-
130-
#[cfg(feature = "native-mux")]
131-
Error::SshMux(ref e) => Some(e),
132-
}
133-
}
134-
}
135-
13695
impl Error {
13796
pub(crate) fn interpret_ssh_error(stderr: &str) -> Self {
13897
// we want to turn the string-only ssh error into something a little more "handleable".

0 commit comments

Comments
 (0)