diff options
author | Vika <vika@fireburn.ru> | 2021-05-04 21:14:31 +0300 |
---|---|---|
committer | Vika <vika@fireburn.ru> | 2021-05-04 21:14:31 +0300 |
commit | 1a78c48bd86ff72cc9c7020e69ec9a77302a2f8b (patch) | |
tree | 7a0bbd3f520d0eecdb8bcadc23e62f7f1f5757f5 /src/database | |
parent | b0763ade5d16a68654256b4cbfd029210981970d (diff) | |
download | kittybox-1a78c48bd86ff72cc9c7020e69ec9a77302a2f8b.tar.zst |
Made StorageErrors directly convertible into Micropub JSON responses
Diffstat (limited to 'src/database')
-rw-r--r-- | src/database/mod.rs | 16 |
1 files changed, 16 insertions, 0 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()) |