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;

/// The callback to a template the user must provide for error pages. This is passed the status code, the error message, the URL of the
/// problematic asset, and a translator if one is available . Many error pages are generated when a translator is not available or
/// couldn't be instantiated, so you'll need to rely on symbols or the like in these cases.
pub type ErrorPageTemplate<G> =
    Box<dyn Fn(String, u16, String, Option<Rc<Translator>>) -> View<G> + Send + Sync>;

/// A type alias for the `HashMap` the user should provide for error pages.
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> {
    /// Creates a new definition of error pages with just a fallback.
    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),
        }
    }
    /// Adds a new page for the given status code. If a page was already defined for the given code, it will be updated by the mechanics of
    /// the internal `HashMap`.
    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));
    }
    /// Adds a new page for the given status code. If a page was already defined for the given code, it will be updated by the mechanics of
    /// the internal `HashMap`. This differs from `.add_page()` in that it takes an `Rc`, which is useful for plugins.
    pub fn add_page_rc(&mut self, status: u16, page: ErrorPageTemplate<G>) {
        self.status_pages.insert(status, page);
    }
    /// Gets the internal template function to render.
    fn get_template_fn(&self, status: u16) -> &ErrorPageTemplate<G> {
        // Check if we have an explicitly defined page for this status code
        // If not, we'll render the fallback page
        match self.status_pages.contains_key(&status) {
            true => self.status_pages.get(&status).unwrap(),
            false => &self.fallback,
        }
    }
    /// Gets the template for a page without rendering it into a container.
    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> {
    /// Renders the appropriate error page to the given DOM container.
    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);
        // Render that to the given container
        sycamore::render_to(
            || template_fn(url.to_string(), status, err.to_string(), translator),
            container,
        );
    }
}
impl ErrorPages<HydrateNode> {
    /// Hydrates the appropriate error page to the given DOM container. This is used for when an error page is rendered by the server
    /// and then needs interactivity.
    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);
        // Render that to the given container
        sycamore::hydrate_to(
            || template_fn(url.to_string(), status, err.to_string(), translator),
            container,
        );
    }
}
impl ErrorPages<SsrNode> {
    /// Renders the error page to a string. This should then be hydrated on the client-side.
    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);
        // Render that to the given container
        sycamore::render_to_string(|| {
            template_fn(url.to_string(), status, err.to_string(), translator)
        })
    }
}
// We provide default error pages to speed up development, but they have to be added before moving to production (or we'll `panic!`)
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)) }
            }
        });
        // 404 is the most common by far, so we add a little page for that too
        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")
    }
}

/// A representation of an error page, particularly for storage in transit so that server-side rendered error pages can be hydrated on
/// the client-side.
#[derive(Serialize, Deserialize, Debug)]
pub struct ErrorPageData {
    /// The URL for the error.
    pub url: String,
    /// THe HTTP status code that corresponds with the error.
    pub status: u16,
    /// The actual error message as a string.
    pub err: String,
}