aboutsummaryrefslogtreecommitdiff
path: root/server/src/ui/error.rs
diff options
context:
space:
mode:
Diffstat (limited to 'server/src/ui/error.rs')
-rw-r--r--server/src/ui/error.rs104
1 files changed, 104 insertions, 0 deletions
diff --git a/server/src/ui/error.rs b/server/src/ui/error.rs
new file mode 100644
index 0000000..c9620bb
--- /dev/null
+++ b/server/src/ui/error.rs
@@ -0,0 +1,104 @@
+/*
+ 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) 2025 metamuffin <metamuffin.org>
+*/
+use super::layout::{DynLayoutPage, LayoutPage};
+use crate::{ui::account::rocket_uri_macro_r_account_login, uri};
+use jellybase::CONF;
+use log::info;
+use rocket::{
+ catch,
+ http::{ContentType, Status},
+ response::{self, Responder},
+ Request,
+};
+use serde_json::{json, Value};
+use std::{fmt::Display, fs::File, io::Read, sync::LazyLock};
+
+static ERROR_IMAGE: LazyLock<Vec<u8>> = LazyLock::new(|| {
+ info!("loading error image");
+ let mut f = File::open(CONF.asset_path.join("error.avif"))
+ .expect("please create error.avif in the asset dir");
+ let mut o = Vec::new();
+ f.read_to_end(&mut o).unwrap();
+ o
+});
+
+#[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" }
+ }
+ },
+ ..Default::default()
+ }
+}
+
+#[catch(default)]
+pub fn r_api_catch(status: Status, _request: &Request) -> Value {
+ json!({ "error": format!("{status}") })
+}
+
+pub type MyResult<T> = Result<T, MyError>;
+
+// TODO an actual error enum would be useful for status codes
+
+pub struct MyError(pub anyhow::Error);
+
+impl<'r> Responder<'r, 'static> for MyError {
+ fn respond_to(self, req: &'r Request<'_>) -> response::Result<'static> {
+ match req.accept().map(|a| a.preferred()) {
+ Some(x) if x.is_json() => json!({ "error": format!("{}", self.0) }).respond_to(req),
+ Some(x) if x.is_avif() || x.is_png() || x.is_jpeg() => {
+ (ContentType::AVIF, ERROR_IMAGE.as_slice()).respond_to(req)
+ }
+ _ => LayoutPage {
+ title: "Error".to_string(),
+ content: markup::new! {
+ h2 { "An error occured. Nobody is sorry"}
+ pre.error { @format!("{:?}", self.0) }
+ },
+ ..Default::default()
+ }
+ .respond_to(req),
+ }
+ }
+}
+
+impl std::fmt::Debug for MyError {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ f.write_fmt(format_args!("{:?}", self.0))
+ }
+}
+
+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}"))
+ }
+}
+impl From<serde_json::Error> for MyError {
+ fn from(err: serde_json::Error) -> Self {
+ MyError(anyhow::anyhow!("{err}"))
+ }
+}