aboutsummaryrefslogtreecommitdiff
path: root/server/src/routes/api/error.rs
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2023-02-19 15:55:34 +0100
committermetamuffin <metamuffin@disroot.org>2023-02-19 15:55:34 +0100
commit5a1ff49fe38451451a55266e86a9e9aedecfb44d (patch)
treefe9ff6f9e30e776c5e8dc5d6d69554cdc2375b1c /server/src/routes/api/error.rs
parent15d0a83247c3b6091f006df967f54f8399030cf6 (diff)
downloadjellything-5a1ff49fe38451451a55266e86a9e9aedecfb44d.tar
jellything-5a1ff49fe38451451a55266e86a9e9aedecfb44d.tar.bz2
jellything-5a1ff49fe38451451a55266e86a9e9aedecfb44d.tar.zst
added api
Diffstat (limited to 'server/src/routes/api/error.rs')
-rw-r--r--server/src/routes/api/error.rs57
1 files changed, 57 insertions, 0 deletions
diff --git a/server/src/routes/api/error.rs b/server/src/routes/api/error.rs
new file mode 100644
index 0000000..ef5374c
--- /dev/null
+++ b/server/src/routes/api/error.rs
@@ -0,0 +1,57 @@
+// TODO: Slightâ„¢ code duplication with `ui/error.rs`
+
+use rocket::{
+ response::{self, Responder},
+ Request,
+};
+use serde_json::{json, Value};
+use std::fmt::Display;
+
+use crate::routes::ui::error::MyError;
+
+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)
+ }
+}