Skip to main content

fluidattacks_core_git/
errors.rs

1use std::io;
2use std::process::ExitStatus;
3use std::time::Duration;
4
5#[derive(Debug, thiserror::Error)]
6pub enum GitCallError {
7    #[error("git {command} could not start: {source}")]
8    NotStarted {
9        command: &'static str,
10        source: io::Error,
11    },
12    #[error("git {command} did not finish within {after:?}")]
13    TimedOut {
14        command: &'static str,
15        after: Duration,
16    },
17    /// git ran and refused, as it does for a missing repository or revision.
18    #[error("git {command} exited with {status}: {stderr}")]
19    Refused {
20        command: &'static str,
21        status: ExitStatus,
22        stderr: String,
23    },
24    /// No process can receive a NUL, so std turns the call down before it starts.
25    #[error("git {command} got an argument with an embedded null byte")]
26    NullByte { command: &'static str },
27    #[error("git {command} printed something unreadable: {reason}")]
28    Malformed {
29        command: &'static str,
30        reason: String,
31    },
32}
33
34/// Why `download_and_extract_repo` failed, by the step that failed.
35#[derive(Debug, thiserror::Error)]
36pub enum DownloadError {
37    /// The parent of the destination, the work directory beside it, or the
38    /// archive inside that directory could not be created or written.
39    #[error("writing the archive: {0}")]
40    Write(#[source] io::Error),
41    #[error(transparent)]
42    Fetch(#[from] FetchError),
43    #[error(transparent)]
44    Extract(#[from] ExtractError),
45    /// `git reset --hard` did not restore the working tree.
46    #[error(transparent)]
47    Reset(#[from] GitCallError),
48    /// Removing symlinks or out-of-scope files failed; `errno` is the one of
49    /// the file call that failed, when there was one.
50    #[error("pruning the working tree: {message}")]
51    Prune { message: String, errno: Option<i32> },
52    /// The repository could not take the place of the destination.
53    #[error("placing the repository: {0}")]
54    Place(#[source] io::Error),
55}
56
57/// How the request for the archive failed.
58#[derive(Debug, thiserror::Error)]
59pub enum FetchError {
60    #[error("invalid url {url}: {reason}")]
61    InvalidUrl { url: String, reason: String },
62    #[error("not an http url: {0}")]
63    NonHttpUrl(String),
64    /// DNS, a refused connection or a TLS handshake that failed.
65    #[error("could not connect: {0}")]
66    Connect(#[source] reqwest::Error),
67    #[error("connecting timed out: {0}")]
68    ConnectTimeout(#[source] reqwest::Error),
69    /// The total timeout ran out before the response headers arrived.
70    #[error("no response in time: {0}")]
71    Timeout(#[source] reqwest::Error),
72    /// The server closed the connection before it answered.
73    #[error("the server disconnected: {0}")]
74    Disconnected(#[source] reqwest::Error),
75    /// The socket failed while the request was sent; `errno` is the one of
76    /// the socket call.
77    #[error("the socket failed: {source}")]
78    Socket {
79        reset: bool,
80        errno: Option<i32>,
81        source: reqwest::Error,
82    },
83    #[error("malformed response: {0}")]
84    Malformed(#[source] reqwest::Error),
85    #[error("the server answered {0}")]
86    Status(reqwest::StatusCode),
87    /// The body ended before its length, or the connection broke mid-read.
88    #[error("the body was cut: {0}")]
89    BodyCut(#[source] reqwest::Error),
90    /// The total timeout ran out while the body was read.
91    #[error("the body did not arrive in time: {0}")]
92    BodyTimeout(#[source] reqwest::Error),
93    #[error("the request failed: {0}")]
94    Other(#[source] reqwest::Error),
95}
96
97/// How turning the archive into a repository failed.
98#[derive(Debug, thiserror::Error)]
99pub enum ExtractError {
100    /// The archive is not gzip, its tar or deflate data is broken, or its
101    /// checksum does not match.
102    #[error("corrupt archive: {0}")]
103    Corrupt(#[source] io::Error),
104    /// The gzip stream ends before its trailer.
105    #[error("truncated archive: {0}")]
106    Truncated(#[source] io::Error),
107    /// The archive holds no top directory with a real `.git` directory.
108    #[error("the archive holds no repository: {0}")]
109    NoRepository(String),
110    #[error("extracting the archive: {0}")]
111    Io(#[source] io::Error),
112}