1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use crate::translator::Translator;
use crate::{DomNode, Html, HydrateNode, SsrNode};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::rc::Rc;
use sycamore::view;
use sycamore::view::View;
use web_sys::Element;
pub type ErrorPageTemplate<G> =
Box<dyn Fn(String, u16, String, Option<Rc<Translator>>) -> View<G> + Send + Sync>;
pub struct ErrorPages<G: Html> {
status_pages: HashMap<u16, ErrorPageTemplate<G>>,
fallback: ErrorPageTemplate<G>,
}
impl<G: Html> std::fmt::Debug for ErrorPages<G> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ErrorPages").finish()
}
}
impl<G: Html> ErrorPages<G> {
pub fn new(
fallback: impl Fn(String, u16, String, Option<Rc<Translator>>) -> View<G>
+ Send
+ Sync
+ 'static,
) -> Self {
Self {
status_pages: HashMap::default(),
fallback: Box::new(fallback),
}
}
pub fn add_page(
&mut self,
status: u16,
page: impl Fn(String, u16, String, Option<Rc<Translator>>) -> View<G> + Send + Sync + 'static,
) {
self.status_pages.insert(status, Box::new(page));
}
pub fn add_page_rc(&mut self, status: u16, page: ErrorPageTemplate<G>) {
self.status_pages.insert(status, page);
}
fn get_template_fn(&self, status: u16) -> &ErrorPageTemplate<G> {
match self.status_pages.contains_key(&status) {
true => self.status_pages.get(&status).unwrap(),
false => &self.fallback,
}
}
pub fn get_template_for_page(
&self,
url: &str,
status: u16,
err: &str,
translator: Option<Rc<Translator>>,
) -> View<G> {
let template_fn = self.get_template_fn(status);
template_fn(url.to_string(), status, err.to_string(), translator)
}
}
impl ErrorPages<DomNode> {
pub fn render_page(
&self,
url: &str,
status: u16,
err: &str,
translator: Option<Rc<Translator>>,
container: &Element,
) {
let template_fn = self.get_template_fn(status);
sycamore::render_to(
|| template_fn(url.to_string(), status, err.to_string(), translator),
container,
);
}
}
impl ErrorPages<HydrateNode> {
pub fn hydrate_page(
&self,
url: &str,
status: u16,
err: &str,
translator: Option<Rc<Translator>>,
container: &Element,
) {
let template_fn = self.get_template_fn(status);
sycamore::hydrate_to(
|| template_fn(url.to_string(), status, err.to_string(), translator),
container,
);
}
}
impl ErrorPages<SsrNode> {
pub fn render_to_string(
&self,
url: &str,
status: u16,
err: &str,
translator: Option<Rc<Translator>>,
) -> String {
let template_fn = self.get_template_fn(status);
sycamore::render_to_string(|| {
template_fn(url.to_string(), status, err.to_string(), translator)
})
}
}
impl<G: Html> Default for ErrorPages<G> {
#[cfg(debug_assertions)]
fn default() -> Self {
let mut error_pages = Self::new(|url, status, err, _| {
view! {
p { (format!("An error with HTTP code {} occurred at '{}': '{}'.", status, url, err)) }
}
});
error_pages.add_page(404, |_, _, _, _| {
view! {
p { "Page not found." }
}
});
error_pages
}
#[cfg(not(debug_assertions))]
fn default() -> Self {
panic!("you must provide your own error pages in production")
}
}
#[derive(Serialize, Deserialize, Debug)]
pub struct ErrorPageData {
pub url: String,
pub status: u16,
pub err: String,
}