Skip to main content

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 that may occur while formatting the changelog as Markdown.
14    #[error("Markdown format error: `{0}`")]
15    MarkdownFormatError(#[from] pulldown_cmark_to_cmark::Error),
16    /// Error variant that represents errors coming out of libgit2.
17    #[cfg(feature = "repo")]
18    #[error("Git error: `{0}`")]
19    GitError(#[from] git2::Error),
20    /// Error that may occur when failed to set a commit range.
21    #[cfg(feature = "repo")]
22    #[error(
23        "Failed to set the commit range: {1}
24{0:?} is not a valid commit range. Did you provide the correct arguments?"
25    )]
26    SetCommitRangeError(String, #[source] git2::Error),
27    /// Error variant that represents other repository related errors.
28    #[cfg(feature = "repo")]
29    #[error("Git repository error: `{0}`")]
30    RepoError(String),
31    /// Error that may occur while parsing the config file.
32    #[error("Cannot parse config: `{0}`")]
33    ConfigError(#[from] config::ConfigError),
34    /// A possible error while initializing the logger.
35    #[error("Logger error: `{0}`")]
36    LoggerError(String),
37    /// When commit's not follow the conventional commit structure we throw this
38    /// error.
39    #[error("Commit did not match conventional format: `{0}`")]
40    ParseError(#[from] git_conventional::Error),
41    /// Error that may occur while grouping commits.
42    #[error("Grouping error: `{0}`")]
43    GroupError(String),
44    /// Error that may occur while generating changelog.
45    #[error("Changelog error: `{0}`")]
46    ChangelogError(String),
47    /// Error that may occur while parsing the template.
48    #[error("Template parse error:\n{0}")]
49    TemplateParseError(String),
50    /// Error that may occur while rendering the template.
51    #[error("Template render error:\n{0}")]
52    TemplateRenderError(String),
53    /// Error that may occur while rendering the template.
54    #[error("Template render error:\n{0}\n{1}")]
55    TemplateRenderDetailedError(String, String),
56    /// Error that may occur during more general template operations.
57    #[error("Template error: `{0}`")]
58    TemplateError(#[from] tera::Error),
59    /// Error that may occur while parsing the command line arguments.
60    #[error("Argument error: `{0}`")]
61    ArgumentError(String),
62    /// Error that may occur while extracting the embedded content.
63    #[error("Embedded error: `{0}`")]
64    EmbeddedError(String),
65    /// Errors that may occur when deserializing types from TOML format.
66    #[error("Cannot parse TOML: `{0}`")]
67    DeserializeError(#[from] toml::de::Error),
68    /// Errors that may occur while de/serializing JSON format.
69    #[error("Cannot de/serialize JSON: `{0}`")]
70    JsonError(#[from] serde_json::Error),
71    /// Errors that may occur during parsing or compiling a regular expression.
72    #[error("Cannot parse/compile regex: `{0}`")]
73    RegexError(#[from] regex::Error),
74    /// Error that may occur due to system time related anomalies.
75    #[error("System time error: `{0}`")]
76    SystemTimeError(#[from] std::time::SystemTimeError),
77    /// Error that may occur while parsing integers.
78    #[error("Failed to parse integer: `{0}`")]
79    IntParseError(#[from] std::num::TryFromIntError),
80    /// Error that may occur while processing parsers that define field and
81    /// value matchers.
82    #[error("Field error: `{0}`")]
83    FieldError(String),
84    /// Error that may occur while parsing a `SemVer` version or version
85    /// requirement.
86    #[error("Semver error: `{0}`")]
87    SemverError(#[from] semver::Error),
88    /// The errors that may occur when processing a HTTP request.
89    #[error("HTTP client error: `{0}`")]
90    #[cfg(feature = "remote")]
91    HttpClientError(#[from] reqwest::Error),
92    /// The errors that may occur while constructing the HTTP client with
93    /// middleware.
94    #[error("HTTP client with middleware error: `{0}`")]
95    #[cfg(feature = "remote")]
96    HttpClientMiddlewareError(#[from] reqwest_middleware::Error),
97    /// A possible error when converting a `HeaderValue` from a string or byte
98    /// slice.
99    #[error("HTTP header error: `{0}`")]
100    #[cfg(feature = "remote")]
101    HttpHeaderError(#[from] reqwest::header::InvalidHeaderValue),
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    /// Error raised when commits are not matched by any commit parser and the
119    /// [`crate::config::GitConfig::fail_on_unmatched_commit`] option is enabled.
120    #[error("Found {0} unmatched commit(s)")]
121    UnmatchedCommitsError(i32),
122}
123
124/// Result type of the core library.
125pub type Result<T> = core::result::Result<T, Error>;
126
127#[cfg(test)]
128mod test {
129    use git_conventional::{Commit, ErrorKind};
130
131    use super::*;
132    fn mock_function() -> super::Result<Commit<'static>> {
133        Ok(Commit::parse("test")?)
134    }
135
136    #[test]
137    fn throw_parse_error() {
138        let actual_error = mock_function().expect_err("expected error");
139        let expected_error_kind = ErrorKind::MissingType;
140        match actual_error {
141            Error::ParseError(e) => {
142                assert_eq!(expected_error_kind, e.kind());
143            }
144            _ => {
145                unreachable!()
146            }
147        }
148    }
149}