/* This file is part of jellything (https://codeberg.org/metamuffin/jellything) which is licensed under the GNU Affero General Public License (version 3); see /COPYING. Copyright (C) 2023 metamuffin */ use super::layout::{DynLayoutPage, LayoutPage}; use crate::{routes::ui::account::rocket_uri_macro_r_account_login, uri}; use rocket::{ catch, http::Status, response::{self, Responder}, Request, }; use std::fmt::Display; #[catch(default)] pub fn r_catch<'a>(status: Status, _request: &Request) -> DynLayoutPage<'a> { LayoutPage { title: "Not found".to_string(), content: markup::new! { h2 { "Error" } p { @format!("{status}") } @if status == Status::NotFound { p { "You might need to " a[href=uri!(r_account_login())] { "log in" } ", to see this page" } } }, } } pub type MyResult = Result; #[derive(Debug)] pub struct MyError(pub anyhow::Error); impl<'r> Responder<'r, 'static> for MyError { fn respond_to(self, req: &'r Request<'_>) -> response::Result<'static> { LayoutPage { title: "Error".to_string(), content: markup::new! { h2 { "An error occured. Nobody is sorry"} pre.error { @format!("{:?}", self.0) } }, } .respond_to(req) } } impl Display for MyError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { self.0.fmt(f) } } impl From for MyError { fn from(err: anyhow::Error) -> MyError { MyError(err) } } impl From for MyError { fn from(err: std::fmt::Error) -> MyError { MyError(anyhow::anyhow!("{err}")) } } impl From for MyError { fn from(err: std::io::Error) -> Self { MyError(anyhow::anyhow!("{err}")) } } impl From for MyError { fn from(err: sled::Error) -> Self { MyError(anyhow::anyhow!("{err}")) } }