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
54/// Errors related to invoking clang tools and processing their output.
55#[derive(Debug, thiserror::Error)]
56pub enum ClangCaptureError {
57    /// Error when failing to acquire a lock on a file's mutex.
58    #[error("Failed to acquire a lock on a file's mutex")]
59    MutexPoisoned,
60
61    /// Error when invoking a clang tool with no known path to the binary executable.
62    #[error("Unknown path to {0} tool; required to invoke it.")]
63    ToolPathUnknown(&'static str),
64
65    /// Error when a clang tool fails to be invoked.
66    #[error("Failed to {task}: {source}")]
67    FailedToRunCommand {
68        /// The purpose of running the clang tool.
69        ///
70        /// May include context about the arguments passed to the clang-tool.
71        task: String,
72
73        /// The underlying error from trying to run the clang tool.
74        #[source]
75        source: std::io::Error,
76    },
77
78    /// Error when the output of a clang tool cannot be parsed as a UTF-8 string.
79    #[error("{task} output was not valid UTF-8: {source}")]
80    NonUtf8Output {
81        /// The clang tool that produced the output.
82        task: String,
83
84        /// The underlying error from trying to convert the clang tool's output to a UTF-8 string.
85        #[source]
86        source: std::string::FromUtf8Error,
87    },
88
89    /// Error when failing to read a file's contents.
90    #[error("Failed to read contents of file '{file_name}': {source}")]
91    ReadFileFailed {
92        /// The name of the file that failed to be read.
93        file_name: String,
94
95        /// The underlying error from trying to read the file's contents.
96        #[source]
97        source: std::io::Error,
98    },
99
100    /// Error when failing to write a file.
101    #[error("Failed to write file '{file_name}': {source}")]
102    WriteFileFailed {
103        /// The name of the file that failed to be written.
104        file_name: String,
105
106        /// The underlying error from trying to write the file.
107        #[source]
108        source: std::io::Error,
109    },
110
111    /// Error when failing to compile a regular expression pattern.
112    #[error("Failed to compile regular expression: {0}")]
113    RegexError(#[from] regex::Error),
114
115    /// Error when failing to determine the current working directory.
116    #[error("Failed to determine the current working directory: {0}")]
117    UnknownWorkingDirectory(#[source] std::io::Error),
118
119    /// Error when failing to parse an integer from a string.
120    #[error("Failed to parse integer from string: {0}")]
121    ParseIntError(#[from] std::num::ParseIntError),
122
123    /// Error when failing to determine the parent directory for caching purposes.
124    #[error("Failed to determine the parent directory for caching purposes")]
125    UnknownCacheParentPath,
126
127    /// Error when failing to create a directory for caching purposes.
128    #[error("Failed to create directory for caching patches: {0}")]
129    MkDirFailed(#[source] std::io::Error),
130}
131
132/// Errors related to orchestrating clang tools in parallel.
133#[derive(Debug, thiserror::Error)]
134pub enum ClangTaskError {
135    /// Error to propagate failures from downloading/installing/finding a clang tool.
136    #[error(transparent)]
137    GetToolError(#[from] GetToolError),
138
139    /// Error when the tool manager cannot find a suitable version of a clang tool.
140    #[error("Failed to find tool {0} or install a suitable version")]
141    FindToolError(&'static str),
142
143    /// Error when failing to parse the compilation database.
144    ///
145    /// This can occur regardless of invoking clang-tidy.
146    #[error("Failed to parse compilation database: {0}")]
147    ParseJsonError(#[from] serde_json::Error),
148
149    /// Error to propagate task joining failures (from the tokio runtime).
150    #[error("Failed to execute task in parallel: {0}")]
151    JoinError(#[from] tokio::task::JoinError),
152
153    /// Error to propagate failures from capturing clang tools' output.
154    #[error(transparent)]
155    ClangCaptureError(#[from] ClangCaptureError),
156}