Skip to content

Commit

Permalink
feat(template): use more explanatory error messages about templates
Browse files Browse the repository at this point in the history
  • Loading branch information
orhun committed Dec 8, 2021
1 parent 7f867ae commit 1a9c3e3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
8 changes: 7 additions & 1 deletion git-cliff-core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ pub enum Error {
/// Error that may occur while generating changelog.
#[error("Changelog error: `{0}`")]
ChangelogError(String),
/// Error that may occur while template operations such as parse and render.
/// Error that may occur while parsing the template.
#[error("Template parse error:\n{0}")]
TemplateParseError(String),
/// Error that may occur while rendering the template.
#[error("Template render error:\n{0}")]
TemplateRenderError(String),
/// Error that may occur during more general template operations.
#[error("Template error: `{0}`")]
TemplateError(#[from] tera::Error),
/// Error that may occur while parsing the command line arguments.
Expand Down
28 changes: 23 additions & 5 deletions git-cliff-core/src/template.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use crate::error::Result;
use crate::error::{
Error,
Result,
};
use crate::release::Release;
use std::collections::HashMap;
use std::error::Error as ErrorImpl;
use tera::{
Context as TeraContext,
Result as TeraResult,
Expand All @@ -18,7 +22,13 @@ impl Template {
/// Constructs a new instance.
pub fn new(template: String) -> Result<Self> {
let mut tera = Tera::default();
tera.add_raw_template("template", &template)?;
if let Err(e) = tera.add_raw_template("template", &template) {
return if let Some(error_source) = e.source() {
Err(Error::TemplateParseError(error_source.to_string()))
} else {
Err(Error::TemplateError(e))
};
}
tera.register_filter("upper_first", Self::upper_first_filter);
Ok(Self { tera })
}
Expand All @@ -40,9 +50,17 @@ impl Template {

/// Renders the template.
pub fn render(&self, release: &Release) -> Result<String> {
Ok(self
.tera
.render("template", &TeraContext::from_serialize(release)?)?)
let context = TeraContext::from_serialize(release)?;
match self.tera.render("template", &context) {
Ok(v) => Ok(v),
Err(e) => {
return if let Some(error_source) = e.source() {
Err(Error::TemplateRenderError(error_source.to_string()))
} else {
Err(Error::TemplateError(e))
};
}
}
}
}

Expand Down

0 comments on commit 1a9c3e3

Please sign in to comment.