diff options
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()) |