about summary refs log tree commit diff
path: root/kittybox-rs/src/micropub
diff options
context:
space:
mode:
Diffstat (limited to 'kittybox-rs/src/micropub')
-rw-r--r--kittybox-rs/src/micropub/mod.rs79
1 files changed, 2 insertions, 77 deletions
diff --git a/kittybox-rs/src/micropub/mod.rs b/kittybox-rs/src/micropub/mod.rs
index 1fa442a..1d81505 100644
--- a/kittybox-rs/src/micropub/mod.rs
+++ b/kittybox-rs/src/micropub/mod.rs
@@ -11,6 +11,8 @@ use serde_json::json;
 use std::fmt::Display;
 use tracing::{debug, error, info, warn};
 
+use kittybox_util::{MicropubError, ErrorType};
+
 #[derive(Serialize, Deserialize, Debug, PartialEq)]
 #[serde(rename_all = "kebab-case")]
 enum QueryType {
@@ -26,25 +28,6 @@ pub struct MicropubQuery {
     url: Option<String>,
 }
 
-#[derive(Serialize, Deserialize, PartialEq, Debug)]
-#[serde(rename_all = "snake_case")]
-pub(crate) enum ErrorType {
-    AlreadyExists,
-    Forbidden,
-    InternalServerError,
-    InvalidRequest,
-    InvalidScope,
-    NotAuthorized,
-    NotFound,
-    UnsupportedMediaType,
-}
-
-#[derive(Serialize, Deserialize, Debug)]
-pub(crate) struct MicropubError {
-    pub(crate) error: ErrorType,
-    pub(crate) error_description: String,
-}
-
 impl From<StorageError> for MicropubError {
     fn from(err: StorageError) -> Self {
         Self {
@@ -57,64 +40,6 @@ impl From<StorageError> for MicropubError {
     }
 }
 
-impl std::error::Error for MicropubError {}
-
-impl Display for MicropubError {
-    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
-        f.write_str("Micropub error: ")?;
-        f.write_str(&self.error_description)
-    }
-}
-
-impl From<&MicropubError> for StatusCode {
-    fn from(err: &MicropubError) -> Self {
-        use ErrorType::*;
-        match err.error {
-            AlreadyExists => StatusCode::CONFLICT,
-            Forbidden => StatusCode::FORBIDDEN,
-            InternalServerError => StatusCode::INTERNAL_SERVER_ERROR,
-            InvalidRequest => StatusCode::BAD_REQUEST,
-            InvalidScope => StatusCode::UNAUTHORIZED,
-            NotAuthorized => StatusCode::UNAUTHORIZED,
-            NotFound => StatusCode::NOT_FOUND,
-            UnsupportedMediaType => StatusCode::UNSUPPORTED_MEDIA_TYPE,
-        }
-    }
-}
-impl From<MicropubError> for StatusCode {
-    fn from(err: MicropubError) -> Self {
-        (&err).into()
-    }
-}
-
-impl axum::response::IntoResponse for MicropubError {
-    fn into_response(self) -> axum::response::Response {
-        axum::response::IntoResponse::into_response((
-            StatusCode::from(&self),
-            axum::response::Json(self),
-        ))
-    }
-}
-
-impl From<serde_json::Error> for MicropubError {
-    fn from(err: serde_json::Error) -> Self {
-        use ErrorType::*;
-        Self {
-            error: InvalidRequest,
-            error_description: err.to_string(),
-        }
-    }
-}
-
-impl MicropubError {
-    fn new(error: ErrorType, error_description: &str) -> Self {
-        Self {
-            error,
-            error_description: error_description.to_owned(),
-        }
-    }
-}
-
 mod util;
 pub(crate) use util::normalize_mf2;