From 1a78c48bd86ff72cc9c7020e69ec9a77302a2f8b Mon Sep 17 00:00:00 2001 From: Vika Date: Tue, 4 May 2021 21:14:31 +0300 Subject: Made StorageErrors directly convertible into Micropub JSON responses --- src/database/mod.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'src/database') 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 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()) -- cgit 1.4.1