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/api/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/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) - } -} |