Skip to main content

cpp_linter/
error.rs

1//! This module defines error types for the cpp-linter crate.
2
3use clang_tools_manager::GetToolError;
4use git_bot_feedback::RestClientError;
5
6/// Errors related to [`FileObj`](crate::common_fs::FileObj) methods' results.
7#[derive(Debug, thiserror::Error)]
8pub enum FileObjError {
9    /// Error when failing to read a file's contents.
10    #[error("Failed to read file contents")]
11    ReadFile(std::io::Error),
12
13    /// Error when failing to convert a file's contents to a UTF-8 string.
14    #[error("Failed to convert patch buffer to UTF-8 string for file {0}: {1}")]
15    FromUtf8Error(String, #[source] std::string::FromUtf8Error),
16
17    /// Error when failing to generate a patch for a file.
18    #[error("Failed to print a hunk to a string buffer: {0}")]
19    DisplayStringFailed(#[source] std::fmt::Error),
20
21    /// Error when failing to create the cache directory for the patch file.
22    #[error("Failed to create cache directory for the patch file: {0}")]
23    MkDirFailed(#[source] std::io::Error),
24
25    /// Error when failing to open the patch file for writing.
26    #[error("Failed to open patch file for writing: {0}")]
27    OpenPatchFileFailed(#[source] std::io::Error),
28
29    /// Error when failing to write to the patch file.
30    #[error("Failed to write to the patch file: {0}")]
31    WritePatchFailed(#[source] std::io::Error),
32}
33
34/// Errors related to the REST client used for posting feedback and special logging.
35#[derive(Debug, thiserror::Error)]
36pub enum ClientError {
37    /// Error to propagate client failures.
38    #[error(transparent)]
39    RestClientError(#[from] RestClientError),
40
41    /// Error when the client cannot detect a supported Git server or CI platform.
42    #[error("Unsupported Git server or CI platform")]
43    GitServerUnsupported,
44
45    /// Error when the client encounters a poisoned mutex during file processing.
46    #[error("Mutex lock poisoned for a source file: {0}")]
47    MutexPoisoned(String),
48
49    /// Error to propagate a [`FileObjError`] encountered during file processing.
50    #[error(transparent)]
51    FileObjError(#[from] FileObjError),
52
53    /// Error when failing to write a given summary output file.
54    #[error("Failed to write summary comment to file '{file_path:?}': {source}")]
55    SummaryOutputFileWriteFailed {
56        /// The path to the summary output file that failed to be written.
57        file_path: std::path::PathBuf,
58
59        /// The underlying error from trying to write the summary output file.
60        #[source]
61        source: std::io::Error,
62    },
63
64    /// Error when failing to determine the parent directory of a given file path.
65    #[error("Failed to create a parent directory for the path '{file_path:?}'")]
66    MkDirFailed {
67        /// The path to the summary output file that failed to be written.
68        file_path: std::path::PathBuf,
69        /// The underlying error from trying to create the parent directory.
70        #[source]
71        source: std::io::Error,
72    },
73}
74
75/// Errors related to invoking clang tools and processing their output.
76#[derive(Debug, thiserror::Error)]
77pub enum ClangCaptureError {
78    /// Error when failing to acquire a lock on a file's mutex.
79    #[error("Failed to acquire a lock on a file's mutex")]
80    MutexPoisoned,
81
82    /// Error when invoking a clang tool with no known path to the binary executable.
83    #[error("Unknown path to {0} tool; required to invoke it.")]
84    ToolPathUnknown(&'static str),
85
86    /// Error when a clang tool fails to be invoked.
87    #[error("Failed to {task}: {source}")]
88    FailedToRunCommand {
89        /// The purpose of running the clang tool.
90        ///
91        /// May include context about the arguments passed to the clang-tool.
92        task: String,
93
94        /// The underlying error from trying to run the clang tool.
95        #[source]
96        source: std::io::Error,
97    },
98
99    /// Error when the output of a clang tool cannot be parsed as a UTF-8 string.
100    #[error("{task} output was not valid UTF-8: {source}")]
101    NonUtf8Output {
102        /// The clang tool that produced the output.
103        task: String,
104
105        /// The underlying error from trying to convert the clang tool's output to a UTF-8 string.
106        #[source]
107        source: std::string::FromUtf8Error,
108    },
109
110    /// Error when failing to read a file's contents.
111    #[error("Failed to read contents of file '{file_name}': {source}")]
112    ReadFileFailed {
113        /// The name of the file that failed to be read.
114        file_name: String,
115
116        /// The underlying error from trying to read the file's contents.
117        #[source]
118        source: std::io::Error,
119    },
120
121    /// Error when failing to write a file.
122    #[error("Failed to write file '{file_name}': {source}")]
123    WriteFileFailed {
124        /// The name of the file that failed to be written.
125        file_name: String,
126
127        /// The underlying error from trying to write the file.
128        #[source]
129        source: std::io::Error,
130    },
131
132    /// Error when failing to compile a regular expression pattern.
133    #[error("Failed to compile regular expression: {0}")]
134    RegexError(#[from] regex::Error),
135
136    /// Error when failing to determine the current working directory.
137    #[error("Failed to determine the current working directory: {0}")]
138    UnknownWorkingDirectory(#[source] std::io::Error),
139
140    /// Error when failing to parse an integer from a string.
141    #[error("Failed to parse integer from string: {0}")]
142    ParseIntError(#[from] std::num::ParseIntError),
143
144    /// Error when failing to determine the parent directory for caching purposes.
145    #[error("Failed to determine the parent directory for caching purposes")]
146    UnknownCacheParentPath,
147
148    /// Error when failing to create a directory for caching purposes.
149    #[error("Failed to create directory for caching patches: {0}")]
150    MkDirFailed(#[source] std::io::Error),
151}
152
153/// Errors related to orchestrating clang tools in parallel.
154#[derive(Debug, thiserror::Error)]
155pub enum ClangTaskError {
156    /// Error to propagate failures from downloading/installing/finding a clang tool.
157    #[error(transparent)]
158    GetToolError(#[from] GetToolError),
159
160    /// Error when the tool manager cannot find a suitable version of a clang tool.
161    #[error("Failed to find tool {0} or install a suitable version")]
162    FindToolError(&'static str),
163
164    /// Error when failing to parse the compilation database.
165    ///
166    /// This can occur regardless of invoking clang-tidy.
167    #[error("Failed to parse compilation database: {0}")]
168    ParseJsonError(#[from] serde_json::Error),
169
170    /// Error to propagate task joining failures (from the tokio runtime).
171    #[error("Failed to execute task in parallel: {0}")]
172    JoinError(#[from] tokio::task::JoinError),
173
174    /// Error to propagate failures from capturing clang tools' output.
175    #[error(transparent)]
176    ClangCaptureError(#[from] ClangCaptureError),
177}