aboutsummaryrefslogtreecommitdiff
path: root/server/src/routes/ui/error.rs
blob: 1a507964e1fed60990c3f53b9f357e387a9625e1 (plain)
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
use super::{layout::Layout, HtmlTemplate};
use markup::Render;
use rocket::http::Status;
use rocket::{
    catch,
    http::ContentType,
    response::{self, Responder},
    Request, Response,
};
use std::{fmt::Display, io::Cursor};

#[catch(default)]
pub fn r_not_found<'a>(status: Status, _request: &Request) -> HtmlTemplate<markup::DynRender<'a>> {
    HtmlTemplate(
        "Not found".to_string(),
        markup::new! {
            h2 { "Error" }
            p { @format!("{status:?}") }
        },
    )
}

pub type MyResult<T> = Result<T, MyError>;

#[derive(Debug)]
pub struct MyError(anyhow::Error);

impl<'r> Responder<'r, 'static> for MyError {
    fn respond_to(self, _: &'r Request<'_>) -> response::Result<'static> {
        let mut out = String::new();
        Layout {
            title: "Error".to_string(),
            main: markup::new! {
                h2 { "An error occured. Nobody is sorry"}
                pre.error { @format!("{:?}", self.0) }
            },
        }
        .render(&mut out)
        .unwrap();
        Response::build()
            .header(ContentType::HTML)
            .streamed_body(Cursor::new(out))
            .ok()
    }
}

impl Display for MyError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.0.fmt(f)
    }
}
impl From<anyhow::Error> for MyError {
    fn from(err: anyhow::Error) -> MyError {
        MyError(err)
    }
}
impl From<std::fmt::Error> for MyError {
    fn from(err: std::fmt::Error) -> MyError {
        MyError(anyhow::anyhow!("{err}"))
    }
}
impl From<std::io::Error> for MyError {
    fn from(err: std::io::Error) -> Self {
        MyError(anyhow::anyhow!("{err}"))
    }
}