about summary refs log tree commit diff
diff options
context:
space:
mode:
authorVika <vika@fireburn.ru>2021-05-04 21:14:31 +0300
committerVika <vika@fireburn.ru>2021-05-04 21:14:31 +0300
commit1a78c48bd86ff72cc9c7020e69ec9a77302a2f8b (patch)
tree7a0bbd3f520d0eecdb8bcadc23e62f7f1f5757f5
parentb0763ade5d16a68654256b4cbfd029210981970d (diff)
downloadkittybox-1a78c48bd86ff72cc9c7020e69ec9a77302a2f8b.tar.zst
Made StorageErrors directly convertible into Micropub JSON responses
-rw-r--r--src/database/mod.rs16
-rw-r--r--src/micropub/get.rs15
-rw-r--r--src/micropub/post.rs6
3 files changed, 22 insertions, 15 deletions
diff --git a/src/database/mod.rs b/src/database/mod.rs
index e676008..1708cff 100644
--- a/src/database/mod.rs
+++ b/src/database/mod.rs
@@ -36,6 +36,22 @@ pub struct StorageError {
 }
 unsafe impl Send for StorageError {}
 unsafe impl Sync for StorageError {}
+impl From<StorageError> for tide::Response {
+    fn from(err: StorageError) -> Self {
+        tide::Response::builder(match err.kind() {
+            ErrorKind::BadRequest => 400,
+            ErrorKind::NotFound => 404,
+            _ => 500,
+        }).body(serde_json::json!({
+            "error": match err.kind() {
+                ErrorKind::BadRequest => "invalid_request",
+                ErrorKind::NotFound => "not_found",
+                _ => "database_error"
+            },
+            "error_description": err
+        })).build()
+    }
+}
 impl std::error::Error for StorageError {
     fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
         self.source.as_ref().map(|e| e.as_ref())
diff --git a/src/micropub/get.rs b/src/micropub/get.rs
index 9a12316..2b367fd 100644
--- a/src/micropub/get.rs
+++ b/src/micropub/get.rs
@@ -23,10 +23,7 @@ where
             let channels: Vec<MicropubChannel>;
             match backend.get_channels(&user).await {
                 Ok(chans) => channels = chans,
-                Err(err) => return Ok(Response::builder(500).body(json!({
-                    "error": "database_error",
-                    "error_description": format!("Couldn't fetch channel list from the database: {:?}", err)
-                })).build())
+                Err(err) => return Ok(err.into())
             }
             Ok(Response::builder(200).body(json!({
                 "q": ["source", "config", "channel"],
@@ -38,10 +35,7 @@ where
             let channels: Vec<MicropubChannel>;
             match backend.get_channels(&user).await {
                 Ok(chans) => channels = chans,
-                Err(err) => return Ok(Response::builder(500).body(json!({
-                    "error": "database_error",
-                    "error_description": format!("Couldn't fetch channel list from the database: {:?}", err)
-                })).build())
+                Err(err) => return Ok(err.into())
             }
             return Ok(Response::builder(200).body(json!(channels)).build())
         }
@@ -54,10 +48,7 @@ where
                         } else {
                             return Ok(Response::builder(404).build())
                         },
-                        Err(err) => return Ok(Response::builder(500).body(json!({
-                            "error": "database_error",
-                            "error_description": err
-                        })).build())
+                        Err(err) => return Ok(err.into())
                     }
                 } else {
                     return Ok(Response::builder(400).body(json!({
diff --git a/src/micropub/post.rs b/src/micropub/post.rs
index 680f242..7ec3566 100644
--- a/src/micropub/post.rs
+++ b/src/micropub/post.rs
@@ -158,7 +158,7 @@ async fn new_post<S: Storage>(req: Request<ApplicationState<S>>, body: serde_jso
         Ok(exists) => if exists {
             return error_json!(409, "already_exists", format!("A post with the exact same UID already exists in the database: {}", uid))
         },
-        Err(err) => return error_json!(500, "database_error", err)
+        Err(err) => return Ok(err.into())
     }
     // WARNING: WRITE BOUNDARY
     //let mut storage = RwLockUpgradableReadGuard::upgrade(storage).await;
@@ -225,7 +225,7 @@ async fn process_json<S: Storage>(req: Request<ApplicationState<S>>, body: serde
                     return error_json!(401, "insufficient_scope", "You need a `delete` scope to delete posts.")
                 }
                 if let Err(error) = req.state().storage.delete_post(&url).await {
-                    return error_json!(500, "database_error", error)
+                    return Ok(error.into())
                 }
                 return Ok(Response::builder(200).build());
             },
@@ -234,7 +234,7 @@ async fn process_json<S: Storage>(req: Request<ApplicationState<S>>, body: serde
                     return error_json!(401, "insufficient_scope", "You need an `update` scope to update posts.")
                 }
                 if let Err(error) = req.state().storage.update_post(&url, body.clone()).await {
-                    return error_json!(500, "database_error", error)
+                    return Ok(error.into())
                 } else {
                     return Ok(Response::builder(204).build())
                 }