diff options
Diffstat (limited to 'server/src/routes/api/error.rs')
-rw-r--r-- | server/src/routes/api/error.rs | 68 |
1 files changed, 0 insertions, 68 deletions
diff --git a/server/src/routes/api/error.rs b/server/src/routes/api/error.rs deleted file mode 100644 index a1d0588..0000000 --- a/server/src/routes/api/error.rs +++ /dev/null @@ -1,68 +0,0 @@ -/* - 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 <metamuffin.org> -*/ -// TODO: Slightâ„¢ code duplication with `ui/error.rs` - -use crate::routes::ui::error::MyError; -use rocket::{ - catch, - http::Status, - response::{self, Responder}, - Request, -}; -use serde_json::{json, Value}; -use std::fmt::Display; - -#[catch(default)] -pub fn r_api_catch<'a>(status: Status, _request: &Request) -> Value { - json!({ "error": format!("{status}") }) -} - -pub type ApiResult<T> = Result<T, ApiError>; - -#[derive(Debug)] -pub struct ApiError(pub anyhow::Error); - -impl<'r> Responder<'r, 'static> for ApiError { - fn respond_to(self, req: &'r Request<'_>) -> response::Result<'static> { - json!({ "error": format!("{}", self.0) }).respond_to(req) - } -} - -impl Display for ApiError { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - self.0.fmt(f) - } -} -impl From<anyhow::Error> for ApiError { - fn from(err: anyhow::Error) -> ApiError { - ApiError(err) - } -} -impl From<std::fmt::Error> for ApiError { - fn from(err: std::fmt::Error) -> ApiError { - ApiError(anyhow::anyhow!("{err}")) - } -} -impl From<std::io::Error> for ApiError { - fn from(err: std::io::Error) -> Self { - ApiError(anyhow::anyhow!("{err}")) - } -} -impl From<sled::Error> for ApiError { - fn from(err: sled::Error) -> Self { - ApiError(anyhow::anyhow!("{err}")) - } -} -impl From<serde_json::Error> for ApiError { - fn from(err: serde_json::Error) -> Self { - ApiError(anyhow::anyhow!("{err}")) - } -} -impl From<MyError> for ApiError { - fn from(value: MyError) -> Self { - Self(value.0) - } -} |