Skip to main content

webserver_base/assets/
error.rs

1//! Failures from building or writing the asset cache.
2
3use std::path::PathBuf;
4
5/// Why the asset cache could not be built.
6///
7/// All boot-time failures. Lookups afterwards never fail —
8/// [`CacheBuster::get_file`](super::CacheBuster::get_file) falls back to the
9/// unhashed path rather than taking a page down over one image.
10#[derive(Debug, thiserror::Error)]
11pub enum CacheBusterError {
12    /// A directory under the asset root could not be read.
13    #[error("failed to read asset directory `{path}`")]
14    ReadDirectory {
15        path: PathBuf,
16        #[source]
17        source: std::io::Error,
18    },
19
20    /// A file could not be read in order to hash it.
21    #[error("failed to read asset `{path}`")]
22    ReadFile {
23        path: PathBuf,
24        #[source]
25        source: std::io::Error,
26    },
27
28    /// A file could not be renamed to its content-hashed name.
29    #[error("failed to rename asset `{from}` to `{to}`")]
30    Rename {
31        from: PathBuf,
32        to: PathBuf,
33        #[source]
34        source: std::io::Error,
35    },
36
37    /// The static-asset pipeline was run somewhere without a `static/`.
38    #[error(
39        "`{path}` does not exist. The static-asset pipeline runs from the \
40         project root, where `static/` lives."
41    )]
42    MissingStaticDirectory { path: PathBuf },
43
44    /// A frontend shipped no source icon.
45    #[error(
46        "no icon source. Provide `static/image/favicon/favicon.svg`, or a \
47         512x512 `static/image/favicon/favicon-512.png` if the art cannot be \
48         vectorised. The .ico, apple-touch icon and web-manifest icons are \
49         generated from whichever you provide."
50    )]
51    MissingFavicon,
52
53    /// A frontend shipped both icon sources.
54    #[error(
55        "both `favicon.svg` and `favicon-512.png` exist. Delete one: two \
56         sources drift the moment somebody updates only one of them."
57    )]
58    AmbiguousFavicon,
59
60    /// The icon directory holds something that is neither a source nor derived.
61    #[error(
62        "`{directory}` contains {count} file(s) that do not belong:{unexpected}\n\n\
63         Only these are allowed: {allowed}.\n\
64         Provide exactly one source — `favicon.svg`, or a 512x512 \
65         `favicon-512.png` — and let the rest be generated."
66    )]
67    UnexpectedFavicons {
68        directory: &'static str,
69        count: usize,
70        unexpected: String,
71        allowed: String,
72    },
73
74    /// The raster icon source is not the required size.
75    #[error("`{path}` must be exactly {expected}x{expected}, but it is {width}x{height}")]
76    FaviconDimensions {
77        path: PathBuf,
78        expected: u32,
79        width: u32,
80        height: u32,
81    },
82
83    /// An image's dimensions could not be read.
84    #[error("failed to read the dimensions of `{path}`: {reason}")]
85    ReadImage { path: PathBuf, reason: String },
86
87    /// A declared asset is not in the manifest, so its URL would 404.
88    #[error(
89        "{count} declared asset(s) are missing from the manifest, so every URL \
90         pointing at them would 404:{missing}\n\nRun the static-asset pipeline, \
91         or correct the paths."
92    )]
93    UnresolvedAssets { count: usize, missing: String },
94
95    /// A generated icon is absent or the wrong size.
96    #[error("`{path}` should be {expected}x{expected} but is {found}")]
97    IconMismatch {
98        path: String,
99        expected: u32,
100        found: String,
101    },
102
103    /// `favicon.svg` could not be parsed.
104    #[error(
105        "failed to parse `favicon.svg`. Note that it must not rely on system \
106         fonts — convert any text to paths."
107    )]
108    RenderIcon {
109        #[source]
110        source: resvg::usvg::Error,
111    },
112
113    /// A pixel buffer of the requested size could not be allocated.
114    #[error("cannot rasterise an icon at {size}x{size}")]
115    IconDimensions { size: u32 },
116
117    /// A rendered icon could not be encoded. Carried as text because the
118    /// encoder's error type belongs to a crate this one does not name directly.
119    #[error("failed to encode a generated icon: {reason}")]
120    EncodeIcon { reason: String },
121
122    /// A generated icon could not be written.
123    #[error("failed to write the generated icon `{path}`")]
124    WriteIcon {
125        path: PathBuf,
126        #[source]
127        source: std::io::Error,
128    },
129
130    /// The manifest exists but is not valid JSON.
131    #[error("failed to parse the cache manifest at `{path}`")]
132    ParseManifest {
133        path: PathBuf,
134        #[source]
135        source: serde_json::Error,
136    },
137
138    /// A site has static assets but no manifest, so every hashed URL would 404.
139    #[error(
140        "`{path}` is missing but `static/` exists. Run `make gen_static_assets` \
141         — asset hashing happens at build time, not at startup."
142    )]
143    MissingManifest { path: PathBuf },
144
145    /// The cache manifest could not be written.
146    #[error("failed to write the cache manifest to `{path}`")]
147    WriteManifest {
148        path: PathBuf,
149        #[source]
150        source: std::io::Error,
151    },
152
153    /// The manifest could not be serialized.
154    #[error("failed to serialize the cache manifest")]
155    SerializeManifest(#[source] serde_json::Error),
156}