Skip to main content

fluidattacks_core_git/
errors.rs

1use std::io;
2use std::path::PathBuf;
3use std::process::ExitStatus;
4use std::time::Duration;
5
6/// A file call failed at `path`; `source` keeps the errno.
7#[derive(Debug, thiserror::Error)]
8#[error("{action} {}: {source}", path.display())]
9pub struct FileError {
10    pub action: &'static str,
11    pub path: PathBuf,
12    #[source]
13    pub source: io::Error,
14}
15
16/// Removing the symlinks or the out-of-scope files failed.
17#[derive(Debug, thiserror::Error)]
18pub enum PruneError {
19    #[error(transparent)]
20    File(#[from] FileError),
21    /// The git that lists the out-of-scope files failed.
22    #[error(transparent)]
23    Git(#[from] GitCallError),
24}
25
26/// Why `reset_repo` failed.
27#[derive(Debug, thiserror::Error)]
28pub enum ResetError {
29    /// `git reset --hard` failed, as it does for a path that is not the root.
30    #[error(transparent)]
31    Git(#[from] GitCallError),
32    #[error(transparent)]
33    Symlinks(#[from] FileError),
34}
35
36/// Why an `HttpsAccess` could not be built, worded as the python worded it.
37#[derive(Debug, PartialEq, Eq, thiserror::Error)]
38pub enum HttpsAccessError {
39    /// git reads a credential line by line: a line break could add a field, a nul cuts it short.
40    #[error("Invalid credentials: a line break or a nul byte")]
41    CredentialLineBreak,
42    #[error("expected an http or https url")]
43    NotHttp,
44    /// The url stays out of the message: it may carry a user and password.
45    #[error("Invalid url: its host or port could not be parsed")]
46    Unparsable,
47    #[error("expected an https credential")]
48    NotHttpsCredential,
49}
50
51/// Why `clone_to_temp` failed, by the step that failed.
52#[derive(Debug, thiserror::Error)]
53pub enum CloneError {
54    #[error(transparent)]
55    Access(#[from] HttpsAccessError),
56    #[error(transparent)]
57    Git(#[from] GitCallError),
58    /// The mirror cloned but its `.info.json` could not be written, so the clone was removed.
59    #[error(transparent)]
60    Info(#[from] FileError),
61    #[error("the SSH key is not base64: {0}")]
62    Key(#[from] base64::DecodeError),
63    #[error("the SSH key could not be written: {0}")]
64    KeyFile(#[source] io::Error),
65    #[error("the SSH key path cannot be quoted for the shell: {0}")]
66    KeyPath(#[from] shlex::QuoteError),
67    #[error(transparent)]
68    Sts(#[from] StsError),
69}
70
71/// Why no session came out of STS for a role.
72#[derive(Debug, thiserror::Error)]
73pub enum StsError {
74    /// STS answered with an error, as it does for a role that does not trust us.
75    #[error("STS refused the role {arn}: {code}: {message}")]
76    Refused {
77        arn: String,
78        code: String,
79        message: String,
80    },
81    /// No answer came: no network, no local credentials or a timeout.
82    #[error("STS could not be asked for the role {arn}: {reason}")]
83    Unreachable { arn: String, reason: String },
84    #[error("STS answered the role {arn} without credentials")]
85    NoCredentials { arn: String },
86}
87
88#[derive(Debug, thiserror::Error)]
89pub enum GitCallError {
90    #[error("git {command} could not start: {source}")]
91    NotStarted {
92        command: &'static str,
93        source: io::Error,
94    },
95    #[error("git {command} did not finish within {after:?}")]
96    TimedOut {
97        command: &'static str,
98        after: Duration,
99    },
100    /// git ran and refused, as it does for a missing repository or revision.
101    #[error("git {command} exited with {status}: {stderr}")]
102    Refused {
103        command: &'static str,
104        status: ExitStatus,
105        stderr: String,
106    },
107    /// No process can receive a NUL, so std turns the call down before it starts.
108    #[error("git {command} got an argument with an embedded null byte")]
109    NullByte { command: &'static str },
110    #[error("git {command} printed something unreadable: {reason}")]
111    Malformed {
112        command: &'static str,
113        reason: String,
114    },
115}
116
117/// Why `download_and_extract_repo` failed, by the step that failed.
118#[derive(Debug, thiserror::Error)]
119pub enum DownloadError {
120    /// The parent of the destination or the archive could not be created or
121    /// written.
122    #[error("writing the archive: {0}")]
123    Write(#[source] io::Error),
124    #[error(transparent)]
125    Fetch(#[from] FetchError),
126    #[error(transparent)]
127    Extract(#[from] ExtractError),
128    /// `git reset --hard` did not restore the working tree.
129    #[error(transparent)]
130    Reset(#[from] GitCallError),
131    #[error("pruning the working tree: {0}")]
132    Prune(#[from] PruneError),
133    /// The repository could not take the place of the destination, or the
134    /// work directory beside it could not be made.
135    #[error("placing the repository: {0}")]
136    Place(#[source] io::Error),
137}
138
139/// How the request for the archive failed.
140#[derive(Debug, thiserror::Error)]
141pub enum FetchError {
142    #[error("invalid url {url}: {reason}")]
143    InvalidUrl { url: String, reason: String },
144    #[error("not an http url: {0}")]
145    NonHttpUrl(String),
146    /// DNS, a refused connection or a TLS handshake that failed.
147    #[error("could not connect: {0}")]
148    Connect(#[source] reqwest::Error),
149    #[error("connecting timed out: {0}")]
150    ConnectTimeout(#[source] reqwest::Error),
151    /// The total timeout ran out before the response headers arrived.
152    #[error("no response in time: {0}")]
153    Timeout(#[source] reqwest::Error),
154    /// The server closed the connection before it answered.
155    #[error("the server disconnected: {0}")]
156    Disconnected(#[source] reqwest::Error),
157    /// The socket failed while the request was sent; `errno` is the one of
158    /// the socket call.
159    #[error("the socket failed: {source}")]
160    Socket {
161        reset: bool,
162        errno: Option<i32>,
163        source: reqwest::Error,
164    },
165    #[error("malformed response: {0}")]
166    Malformed(#[source] reqwest::Error),
167    #[error("the server answered {0}")]
168    Status(reqwest::StatusCode),
169    /// The body ended before its length, or the connection broke mid-read.
170    #[error("the body was cut: {0}")]
171    BodyCut(#[source] reqwest::Error),
172    /// The total timeout ran out while the body was read.
173    #[error("the body did not arrive in time: {0}")]
174    BodyTimeout(#[source] reqwest::Error),
175    #[error("the request failed: {0}")]
176    Other(#[source] reqwest::Error),
177}
178
179/// How turning the archive into a repository failed.
180#[derive(Debug, thiserror::Error)]
181pub enum ExtractError {
182    /// The archive is not gzip, its tar or deflate data is broken, or its
183    /// checksum does not match.
184    #[error("corrupt archive: {0}")]
185    Corrupt(#[source] io::Error),
186    /// The gzip stream ends before its trailer.
187    #[error("truncated archive: {0}")]
188    Truncated(#[source] io::Error),
189    /// The archive holds no top directory with a real `.git` directory.
190    #[error("the archive holds no repository: {0}")]
191    NoRepository(String),
192    #[error("extracting the archive: {0}")]
193    Io(#[source] io::Error),
194}