openssh_sftp_error/
lib.rs

1#![forbid(unsafe_code)]
2
3use std::{io, num::TryFromIntError, process::ExitStatus};
4
5pub use awaitable_error::Error as AwaitableError;
6pub use openssh_sftp_protocol_error::{
7    ErrMsg as SftpErrMsg, ErrorCode as SftpErrorKind, UnixTimeStampError,
8};
9pub use ssh_format_error::Error as SshFormatError;
10use thiserror::Error as ThisError;
11
12/// Error returned by
13/// [`openssh-sftp-client-lowlevel`](https://docs.rs/openssh-sftp-client-lowlevel)
14/// and [`openssh-sftp-client`](https://docs.rs/openssh-sftp-client)
15#[non_exhaustive]
16#[derive(Debug, ThisError)]
17pub enum Error {
18    /// Server speaks sftp protocol other than protocol 3.
19    #[error("Server does not support sftp protocol v3: It only support sftp protocol newer than {version}.")]
20    UnsupportedSftpProtocol {
21        /// The minimal sftp protocol version the server supported.
22        version: u32,
23    },
24
25    /// Server returned a hello message that is too long.
26    #[error("sftp server returned hello message of length {len}, which is longer than 4096.")]
27    SftpServerHelloMsgTooLong {
28        /// The length of the hello mesage
29        len: u32,
30    },
31
32    /// This error is meant to be a dummy error created by user of this crate
33    /// to indicate that the sftp-server run on remote server failed.
34    ///
35    /// openssh-sftp-client would never return this error.
36    #[error("sftp-server run on remote server failed: {0}.")]
37    SftpServerFailure(ExitStatus),
38
39    /// This error is meant to be a dummy error created by user of this crate
40    /// to indicate that the sftp-server run on remote server failed.
41    ///
42    /// openssh-sftp-client would never return this error.
43    #[error("Background task failed: {0}.")]
44    BackgroundTaskFailure(&'static &'static str),
45
46    /// This error is meant to be a dummy error created by user of this crate
47    /// to indicate that the extension is not supported.
48    ///
49    /// openssh-sftp-client would never return this error.
50    #[error("Unsupported extension {0}.")]
51    UnsupportedExtension(&'static &'static str),
52
53    /// IO Error (Excluding [`io::ErrorKind::WouldBlock`]): {0}.
54    #[error("IO Error (Excluding `io::ErrorKind::WouldBlock`): {0}.")]
55    IOError(#[from] io::Error),
56
57    /// Failed to serialize/deserialize the message: {0}.
58    #[error("Failed to serialize/deserialize the message: {0}.")]
59    FormatError(#[from] SshFormatError),
60
61    /// Error when waiting for response
62    #[error("Error when waiting for response: {0}.")]
63    AwaitableError(#[from] AwaitableError),
64
65    /// Sftp protocol can only send and receive at most [`u32::MAX`] data in one request.
66    #[error("Sftp protocol can only send and receive at most u32::MAX data in one request.")]
67    BufferTooLong(#[from] TryFromIntError),
68
69    /// The response id is invalid.
70    ///
71    /// The user can choose to log this error and continue operation.
72    #[error("The response id {response_id} is invalid.")]
73    InvalidResponseId {
74        /// The invalid response id
75        response_id: u32,
76    },
77
78    /// Raised 2 errors when cleaning up.
79    #[error(transparent)]
80    RecursiveErrors(Box<RecursiveError>),
81
82    /// Raised 3 errors when cleaning up.
83    #[error(transparent)]
84    RecursiveErrors3(Box<RecursiveError3>),
85
86    /// Sftp server error
87    #[error("Sftp server reported error kind {0:#?}, msg: {1}")]
88    SftpError(SftpErrorKind, SftpErrMsg),
89
90    /// Invalid response from the sftp-server
91    #[error("Response from sftp server is invalid: {0}")]
92    InvalidResponse(
93        // Use `&&str` since `&str` takes 16 bytes while `&str` only takes 8 bytes.
94        &'static &'static str,
95    ),
96
97    /// Handle returned by server is longer than the limit 256 bytes specified in sftp v3.
98    #[error("Handle returned by server is longer than the limit 256 bytes specified in sftp v3")]
99    HandleTooLong,
100
101    /// tokio join error
102    #[error("Failed to join tokio task")]
103    TaskJoinError(#[from] tokio::task::JoinError),
104
105    #[cfg(feature = "openssh")]
106    #[error("Failed to create sftp from session: {0}")]
107    RemoteChildSpawnError(#[from] openssh::Error),
108}
109
110#[derive(Debug, ThisError)]
111#[error("OriginalError: {original_error}, curr err raised when cleaning up: {occuring_error}.")]
112pub struct RecursiveError {
113    /// Original error
114    pub original_error: Error,
115
116    /// Current error raised when performing cleanup
117    /// for original error.
118    #[source]
119    pub occuring_error: Error,
120}
121
122#[derive(Debug, ThisError)]
123#[error("err1: {err1}, err2: {err2}, err3: {err3}.")]
124pub struct RecursiveError3 {
125    /// First error raised.
126    pub err1: Error,
127
128    /// Second error raised.
129    pub err2: Error,
130
131    /// Third error raised.
132    #[source]
133    pub err3: Error,
134}