mirror of
https://github.com/askama-rs/askama.git
synced 2025-09-28 13:30:59 +00:00
Show error reason when template could not be read
This commit is contained in:
parent
55e8ff1d46
commit
6a6994670d
@ -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))
|
||||
}
|
||||
}
|
||||
|
||||
|
2
testing/templates/a_file_that_is_actually_a_folder.html/.gitignore
vendored
Normal file
2
testing/templates/a_file_that_is_actually_a_folder.html/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
*
|
||||
!.gitignore
|
8
testing/tests/ui/include-a-folder.rs
Normal file
8
testing/tests/ui/include-a-folder.rs
Normal 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() {
|
||||
}
|
9
testing/tests/ui/include-a-folder.stderr
Normal file
9
testing/tests/ui/include-a-folder.stderr
Normal 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)
|
Loading…
x
Reference in New Issue
Block a user