git_cliff_core/
error.rs

1use thiserror::Error as ThisError;
2
3/// Library related errors that we are exposing to the rest of the workspaces.
4#[derive(Debug, ThisError)]
5pub enum Error {
6    /// Error that may occur while I/O operations.
7    #[error("IO error: `{0}`")]
8    IoError(#[from] std::io::Error),
9    /// Error that may occur when attempting to interpret a sequence of u8 as a
10    /// string.
11    #[error("UTF-8 error: `{0}`")]
12    Utf8Error(#[from] std::str::Utf8Error),
13    /// Error variant that represents errors coming out of libgit2.
14    #[cfg(feature = "repo")]
15    #[error("Git error: `{0}`")]
16    GitError(#[from] git2::Error),
17    /// Error that may occur when failed to set a commit range.
18    #[cfg(feature = "repo")]
19    #[error(
20        "Failed to set the commit range: {1}
21{0:?} is not a valid commit range. Did you provide the correct arguments?"
22    )]
23    SetCommitRangeError(String, #[source] git2::Error),
24    /// Error variant that represents other repository related errors.
25    #[cfg(feature = "repo")]
26    #[error("Git repository error: `{0}`")]
27    RepoError(String),
28    /// Error that may occur while parsing the config file.
29    #[error("Cannot parse config: `{0}`")]
30    ConfigError(#[from] config::ConfigError),
31    /// A possible error while initializing the logger.
32    #[error("Logger error: `{0}`")]
33    LoggerError(String),
34    /// When commit's not follow the conventional commit structure we throw this
35    /// error.
36    #[error("Cannot parse the commit: `{0}`")]
37    ParseError(#[from] git_conventional::Error),
38    /// Error that may occur while grouping commits.
39    #[error("Grouping error: `{0}`")]
40    GroupError(String),
41    /// Error that may occur while generating changelog.
42    #[error("Changelog error: `{0}`")]
43    ChangelogError(String),
44    /// Error that may occur while parsing the template.
45    #[error("Template parse error:\n{0}")]
46    TemplateParseError(String),
47    /// Error that may occur while rendering the template.
48    #[error("Template render error:\n{0}")]
49    TemplateRenderError(String),
50    /// Error that may occur while rendering the template.
51    #[error("Template render error:\n{0}\n{1}")]
52    TemplateRenderDetailedError(String, String),
53    /// Error that may occur during more general template operations.
54    #[error("Template error: `{0}`")]
55    TemplateError(#[from] tera::Error),
56    /// Error that may occur while parsing the command line arguments.
57    #[error("Argument error: `{0}`")]
58    ArgumentError(String),
59    /// Error that may occur while extracting the embedded content.
60    #[error("Embedded error: `{0}`")]
61    EmbeddedError(String),
62    /// Errors that may occur when deserializing types from TOML format.
63    #[error("Cannot parse TOML: `{0}`")]
64    DeserializeError(#[from] toml::de::Error),
65    /// Errors that may occur while de/serializing JSON format.
66    #[error("Cannot de/serialize JSON: `{0}`")]
67    JsonError(#[from] serde_json::Error),
68    /// Errors that may occur during parsing or compiling a regular expression.
69    #[error("Cannot parse/compile regex: `{0}`")]
70    RegexError(#[from] regex::Error),
71    /// Error that may occur due to system time related anomalies.
72    #[error("System time error: `{0}`")]
73    SystemTimeError(#[from] std::time::SystemTimeError),
74    /// Error that may occur while parsing integers.
75    #[error("Failed to parse integer: `{0}`")]
76    IntParseError(#[from] std::num::TryFromIntError),
77    /// Error that may occur while processing parsers that define field and
78    /// value matchers.
79    #[error("Field error: `{0}`")]
80    FieldError(String),
81    /// Error that may occur while parsing a `SemVer` version or version
82    /// requirement.
83    #[error("Semver error: `{0}`")]
84    SemverError(#[from] semver::Error),
85    /// The errors that may occur when processing a HTTP request.
86    #[error("HTTP client error: `{0}`")]
87    #[cfg(feature = "remote")]
88    HttpClientError(#[from] reqwest::Error),
89    /// The errors that may occur while constructing the HTTP client with
90    /// middleware.
91    #[error("HTTP client with middleware error: `{0}`")]
92    #[cfg(feature = "remote")]
93    HttpClientMiddlewareError(#[from] reqwest_middleware::Error),
94    /// A possible error when converting a `HeaderValue` from a string or byte
95    /// slice.
96    #[error("HTTP header error: `{0}`")]
97    #[cfg(feature = "remote")]
98    HttpHeaderError(#[from] reqwest::header::InvalidHeaderValue),
99    /// Error that may occur during handling pages.
100    #[error("Pagination error: `{0}`")]
101    PaginationError(String),
102    /// The errors that may occur while parsing URLs.
103    #[error("URL parse error: `{0}`")]
104    UrlParseError(#[from] url::ParseError),
105    /// Error that may occur when a remote is not set.
106    #[error("Repository remote is not set.")]
107    RemoteNotSetError,
108    /// Error that may occur while handling location of directories.
109    #[error("Directory error: `{0}`")]
110    DirsError(String),
111    /// Error that may occur while constructing patterns.
112    #[error("Pattern error: `{0}`")]
113    PatternError(#[from] glob::PatternError),
114    /// Error that may occur when unconventional commits are found.
115    /// See `require_conventional` option for more information.
116    #[error("Requiring all commits be conventional but found {0} unconventional commits.")]
117    UnconventionalCommitsError(i32),
118}
119
120/// Result type of the core library.
121pub type Result<T> = core::result::Result<T, Error>;
122
123#[cfg(test)]
124mod test {
125    use git_conventional::{Commit, ErrorKind};
126
127    use super::*;
128    fn mock_function() -> super::Result<Commit<'static>> {
129        Ok(Commit::parse("test")?)
130    }
131
132    #[test]
133    fn throw_parse_error() {
134        let actual_error = mock_function().expect_err("expected error");
135        let expected_error_kind = ErrorKind::MissingType;
136        match actual_error {
137            Error::ParseError(e) => {
138                assert_eq!(expected_error_kind, e.kind());
139            }
140            _ => {
141                unreachable!()
142            }
143        }
144    }
145}