diff options
author | metamuffin <metamuffin@disroot.org> | 2023-08-01 19:56:38 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2023-08-01 19:56:38 +0200 |
commit | f7992589cf45c699599a7ee5fc4634c9db16ff87 (patch) | |
tree | 973c2e0bc9d50a9e137f999b3c1f231e8471c4be /server/src/routes/ui/error.rs | |
parent | 551e62a6012284823d6b22a9257c3fae07de7fd9 (diff) | |
download | jellything-f7992589cf45c699599a7ee5fc4634c9db16ff87.tar jellything-f7992589cf45c699599a7ee5fc4634c9db16ff87.tar.bz2 jellything-f7992589cf45c699599a7ee5fc4634c9db16ff87.tar.zst |
error format depends on accept header
Diffstat (limited to 'server/src/routes/ui/error.rs')
-rw-r--r-- | server/src/routes/ui/error.rs | 32 |
1 files changed, 23 insertions, 9 deletions
diff --git a/server/src/routes/ui/error.rs b/server/src/routes/ui/error.rs index 88573a6..190650f 100644 --- a/server/src/routes/ui/error.rs +++ b/server/src/routes/ui/error.rs @@ -7,10 +7,11 @@ use super::layout::{DynLayoutPage, LayoutPage}; use crate::{routes::ui::account::rocket_uri_macro_r_account_login, uri}; use rocket::{ catch, - http::Status, + http::{MediaType, Status}, response::{self, Responder}, Request, }; +use serde_json::{json, Value}; use std::fmt::Display; #[catch(default)] @@ -28,6 +29,11 @@ pub fn r_catch<'a>(status: Status, _request: &Request) -> DynLayoutPage<'a> { } } +#[catch(default)] +pub fn r_api_catch<'a>(status: Status, _request: &Request) -> Value { + json!({ "error": format!("{status}") }) +} + pub type MyResult<T> = Result<T, MyError>; #[derive(Debug)] @@ -35,15 +41,23 @@ 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) } - }, - ..Default::default() + if req + .accept() + .map(|a| a.preferred().exact_eq(&MediaType::JSON)) + .unwrap_or(true) + { + json!({ "error": format!("{}", self.0) }).respond_to(req) + } else { + 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) } - .respond_to(req) } } |