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 project registered a helper whose name is already taken.
27    #[error(
28        "the helper `{name}` is already defined by webserver-base or Handlebars. \
29         Rename the project's helper; if several projects need the same one, \
30         it belongs in the library."
31    )]
32    ReservedHelper { name: String },
33
34    /// A frontend was declared with no page templates to serve.
35    #[error(
36        "`{path}` contains no `.hbs` pages. A frontend must serve at least one \
37         page; add `html/pages/home.hbs`."
38    )]
39    NoPages { path: PathBuf },
40
41    /// A frontend has no 404 template.
42    #[error(
43        "`{path}` is missing. Every frontend serves a 404 with a real 404 \
44         status, so the library declares and routes it — but the page itself \
45         is yours to design."
46    )]
47    MissingNotFoundPage { path: PathBuf },
48
49    /// A template file failed to compile.
50    #[error("failed to compile template `{name}`")]
51    Compile {
52        name: String,
53        #[source]
54        source: Box<HandlebarsTemplateError>,
55    },
56
57    /// A template failed to render — usually strict mode catching a field the
58    /// data did not supply.
59    #[error("failed to render template `{name}`")]
60    Render {
61        name: String,
62        #[source]
63        source: Box<RenderError>,
64    },
65
66    /// A page's JSON-LD document could not be serialized.
67    #[error("failed to serialize the JSON-LD document for page `{page}`")]
68    JsonLd {
69        page: String,
70        #[source]
71        source: serde_json::Error,
72    },
73
74    /// The data handed to a render could not be serialized.
75    #[error("failed to serialize template data for page `{page}`")]
76    Serialize {
77        page: String,
78        #[source]
79        source: serde_json::Error,
80    },
81}