webserver_base/webserver/
error.rs1use std::net::SocketAddr;
4
5use axum::http::StatusCode;
6use axum::response::{IntoResponse, Response};
7use tracing::error;
8
9use crate::env::EnvError;
10
11#[derive(thiserror::Error)]
17pub enum WebServerError {
18 #[error(transparent)]
20 Env(#[from] EnvError),
21
22 #[error("failed to bind to {addr}")]
24 Bind {
25 addr: SocketAddr,
26 #[source]
27 source: std::io::Error,
28 },
29
30 #[cfg(feature = "analytics")]
32 #[error("the browser Sentry DSN is malformed")]
33 SentryDsn(#[from] crate::analytics::SentryDsnParseError),
34
35 #[error("the server stopped unexpectedly")]
37 Serve(#[source] std::io::Error),
38
39 #[cfg(feature = "templates")]
41 #[error(transparent)]
42 Template(#[from] crate::templates::TemplateError),
43
44 #[cfg(feature = "templates")]
46 #[error("cannot render: the server was built without `.templates(..)`")]
47 TemplatesNotConfigured,
48
49 #[cfg(feature = "webserver")]
51 #[error(transparent)]
52 CacheBuster(#[from] crate::assets::CacheBusterError),
53
54 #[cfg(feature = "sitemap")]
56 #[error(transparent)]
57 Sitemap(#[from] crate::sitemap::SitemapError),
58
59 #[cfg(feature = "feed")]
61 #[error(transparent)]
62 Feed(#[from] crate::feed::FeedError),
63
64 #[cfg(feature = "feed")]
70 #[error("cannot serve a feed: the server was built without `.frontend(..)`")]
71 FeedWithoutFrontend,
72
73 #[cfg(feature = "observability")]
75 #[error(transparent)]
76 Observability(#[from] crate::observability::ObservabilityError),
77
78 #[cfg(feature = "pages")]
81 #[error("`.pages(..)` requires `.templates(..)`: the sitemap needs the site's base url")]
82 PagesRequireTemplates,
83
84 #[cfg(feature = "pages")]
87 #[error(
88 "page path `{path}` contains a route parameter; \
89 use `dynamic_page_group` with concrete urls, or mark it unlisted"
90 )]
91 DynamicPagePathHasParameters { path: String },
92
93 #[cfg(feature = "pages")]
97 #[error(
98 "pages `{first}` and `{second}` collide: each page already redirects its \
99 other trailing-slash form, so declare one of them only"
100 )]
101 DuplicatePagePath { first: String, second: String },
102}
103
104impl IntoResponse for WebServerError {
105 fn into_response(self) -> Response {
107 error!("request failed: {self}");
108 (StatusCode::INTERNAL_SERVER_ERROR, "internal server error").into_response()
109 }
110}
111
112impl std::fmt::Debug for WebServerError {
120 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
121 writeln!(f, "{self}")?;
122
123 let mut source: Option<&(dyn std::error::Error + 'static)> =
124 std::error::Error::source(self);
125 while let Some(error) = source {
126 writeln!(f, " caused by: {error}")?;
127 source = error.source();
128 }
129 Ok(())
130 }
131}