Skip to main content

webserver_base/templates/
error.rs

1//! Failures from loading or rendering templates.
2
3use std::path::PathBuf;
4
5use handlebars::{RenderError, TemplateError as HandlebarsTemplateError};
6
7/// Why a template could not be loaded or rendered.
8#[derive(Debug, thiserror::Error)]
9pub enum TemplateError {
10    /// A template directory could not be read.
11    #[error("failed to read template directory `{path}`")]
12    ReadDirectory {
13        path: PathBuf,
14        #[source]
15        source: std::io::Error,
16    },
17
18    /// A project supplied a template whose name this crate owns.
19    #[error(
20        "`{path}` shadows the built-in `{name}` layout, which webserver-base \
21         embeds and every page extends. Rename it — a project may add any other \
22         layout, just not this one."
23    )]
24    ReservedName { name: &'static str, path: PathBuf },
25
26    /// A frontend was declared with no page templates to serve.
27    #[error(
28        "`{path}` contains no `.hbs` pages. A frontend must serve at least one \
29         page; add `html/pages/home.hbs`."
30    )]
31    NoPages { path: PathBuf },
32
33    /// A frontend has no 404 template.
34    #[error(
35        "`{path}` is missing. Every frontend serves a 404, and its data is the \
36         same everywhere, so the library declares it — but the page itself is \
37         yours to design."
38    )]
39    MissingNotFoundPage { path: PathBuf },
40
41    /// A template file failed to compile.
42    #[error("failed to compile template `{name}`")]
43    Compile {
44        name: String,
45        #[source]
46        source: Box<HandlebarsTemplateError>,
47    },
48
49    /// A template failed to render — usually strict mode catching a field the
50    /// data did not supply.
51    #[error("failed to render template `{name}`")]
52    Render {
53        name: String,
54        #[source]
55        source: Box<RenderError>,
56    },
57
58    /// A page's JSON-LD document could not be serialized.
59    #[error("failed to serialize the JSON-LD document for page `{page}`")]
60    JsonLd {
61        page: String,
62        #[source]
63        source: serde_json::Error,
64    },
65
66    /// The data handed to a render could not be serialized.
67    #[error("failed to serialize template data for page `{page}`")]
68    Serialize {
69        page: String,
70        #[source]
71        source: serde_json::Error,
72    },
73}