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 HttpsCloneError {
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}
62
63#[derive(Debug, thiserror::Error)]
64pub enum GitCallError {
65    #[error("git {command} could not start: {source}")]
66    NotStarted {
67        command: &'static str,
68        source: io::Error,
69    },
70    #[error("git {command} did not finish within {after:?}")]
71    TimedOut {
72        command: &'static str,
73        after: Duration,
74    },
75    /// git ran and refused, as it does for a missing repository or revision.
76    #[error("git {command} exited with {status}: {stderr}")]
77    Refused {
78        command: &'static str,
79        status: ExitStatus,
80        stderr: String,
81    },
82    /// No process can receive a NUL, so std turns the call down before it starts.
83    #[error("git {command} got an argument with an embedded null byte")]
84    NullByte { command: &'static str },
85    #[error("git {command} printed something unreadable: {reason}")]
86    Malformed {
87        command: &'static str,
88        reason: String,
89    },
90}
91
92/// Why `download_and_extract_repo` failed, by the step that failed.
93#[derive(Debug, thiserror::Error)]
94pub enum DownloadError {
95    /// The parent of the destination or the archive could not be created or
96    /// written.
97    #[error("writing the archive: {0}")]
98    Write(#[source] io::Error),
99    #[error(transparent)]
100    Fetch(#[from] FetchError),
101    #[error(transparent)]
102    Extract(#[from] ExtractError),
103    /// `git reset --hard` did not restore the working tree.
104    #[error(transparent)]
105    Reset(#[from] GitCallError),
106    #[error("pruning the working tree: {0}")]
107    Prune(#[from] PruneError),
108    /// The repository could not take the place of the destination, or the
109    /// work directory beside it could not be made.
110    #[error("placing the repository: {0}")]
111    Place(#[source] io::Error),
112}
113
114/// How the request for the archive failed.
115#[derive(Debug, thiserror::Error)]
116pub enum FetchError {
117    #[error("invalid url {url}: {reason}")]
118    InvalidUrl { url: String, reason: String },
119    #[error("not an http url: {0}")]
120    NonHttpUrl(String),
121    /// DNS, a refused connection or a TLS handshake that failed.
122    #[error("could not connect: {0}")]
123    Connect(#[source] reqwest::Error),
124    #[error("connecting timed out: {0}")]
125    ConnectTimeout(#[source] reqwest::Error),
126    /// The total timeout ran out before the response headers arrived.
127    #[error("no response in time: {0}")]
128    Timeout(#[source] reqwest::Error),
129    /// The server closed the connection before it answered.
130    #[error("the server disconnected: {0}")]
131    Disconnected(#[source] reqwest::Error),
132    /// The socket failed while the request was sent; `errno` is the one of
133    /// the socket call.
134    #[error("the socket failed: {source}")]
135    Socket {
136        reset: bool,
137        errno: Option<i32>,
138        source: reqwest::Error,
139    },
140    #[error("malformed response: {0}")]
141    Malformed(#[source] reqwest::Error),
142    #[error("the server answered {0}")]
143    Status(reqwest::StatusCode),
144    /// The body ended before its length, or the connection broke mid-read.
145    #[error("the body was cut: {0}")]
146    BodyCut(#[source] reqwest::Error),
147    /// The total timeout ran out while the body was read.
148    #[error("the body did not arrive in time: {0}")]
149    BodyTimeout(#[source] reqwest::Error),
150    #[error("the request failed: {0}")]
151    Other(#[source] reqwest::Error),
152}
153
154/// How turning the archive into a repository failed.
155#[derive(Debug, thiserror::Error)]
156pub enum ExtractError {
157    /// The archive is not gzip, its tar or deflate data is broken, or its
158    /// checksum does not match.
159    #[error("corrupt archive: {0}")]
160    Corrupt(#[source] io::Error),
161    /// The gzip stream ends before its trailer.
162    #[error("truncated archive: {0}")]
163    Truncated(#[source] io::Error),
164    /// The archive holds no top directory with a real `.git` directory.
165    #[error("the archive holds no repository: {0}")]
166    NoRepository(String),
167    #[error("extracting the archive: {0}")]
168    Io(#[source] io::Error),
169}