Show error reason when template could not be read

This commit is contained in:
René Kijewski 2024-07-04 10:59:26 +02:00
parent 55e8ff1d46
commit 6a6994670d
4 changed files with 35 additions and 13 deletions

View File

@ -302,20 +302,23 @@ pub(crate) fn get_template_source(
tpl_path: &Path,
import_from: Option<(&Rc<Path>, &str, &str)>,
) -> Result<String, CompileError> {
if let Ok(mut source) = fs::read_to_string(tpl_path) {
if source.ends_with('\n') {
let _ = source.pop();
match fs::read_to_string(tpl_path) {
Ok(mut source) => {
if source.ends_with('\n') {
let _ = source.pop();
}
Ok(source)
}
Err(err) => {
let msg = format!(
"unable to open template file '{}': {err}",
tpl_path.to_str().unwrap(),
);
let file_info = import_from.map(|(node_file, file_source, node_source)| {
FileInfo::new(node_file, Some(file_source), Some(node_source))
});
Err(CompileError::new(msg, file_info))
}
Ok(source)
} else {
let msg = format!(
"unable to open template file '{}'",
tpl_path.to_str().unwrap()
);
let file_info = import_from.map(|(node_file, file_source, node_source)| {
FileInfo::new(node_file, Some(file_source), Some(node_source))
});
Err(CompileError::new(msg, file_info))
}
}

View File

@ -0,0 +1,2 @@
*
!.gitignore

View File

@ -0,0 +1,8 @@
use rinja::Template;
#[derive(Template)]
#[template(ext = "txt", source = r#"{% include "a_file_that_is_actually_a_folder.html" %}"#)]
struct YouCannotIncludeFolders;
fn main() {
}

View File

@ -0,0 +1,9 @@
error: unable to open template file '$WORKSPACE/target/tests/trybuild/rinja_testing/templates/a_file_that_is_actually_a_folder.html': Is a directory (os error 21)
--> YouCannotIncludeFolders.txt:1:2
" include \"a_file_that_is_actually_a_fold"...
--> tests/ui/include-a-folder.rs:3:10
|
3 | #[derive(Template)]
| ^^^^^^^^
|
= note: this error originates in the derive macro `Template` (in Nightly builds, run with -Z macro-backtrace for more info)