From 2bfff6c05d601eee385a19ca39635fd676494c24 Mon Sep 17 00:00:00 2001
From: Awiteb
Date: Sat, 13 Jul 2024 23:15:09 +0300
Subject: [PATCH] refactor: Refactor server errors
Signed-off-by: Awiteb
---
crates/oxidetalis/src/database/blacklist.rs | 6 +-
.../src/database/in_chat_requests.rs | 15 ++--
.../src/database/out_chat_requests.rs | 24 ++----
crates/oxidetalis/src/database/user.rs | 20 ++---
crates/oxidetalis/src/database/whitelist.rs | 26 +++----
crates/oxidetalis/src/errors.rs | 76 ++++++++-----------
crates/oxidetalis/src/macros.rs | 7 +-
crates/oxidetalis/src/main.rs | 6 +-
crates/oxidetalis/src/routes/errors.rs | 68 +++++++++++++++++
crates/oxidetalis/src/routes/mod.rs | 3 +
crates/oxidetalis/src/routes/user.rs | 2 +-
11 files changed, 147 insertions(+), 106 deletions(-)
create mode 100644 crates/oxidetalis/src/routes/errors.rs
diff --git a/crates/oxidetalis/src/database/blacklist.rs b/crates/oxidetalis/src/database/blacklist.rs
index a35eccb..8448843 100644
--- a/crates/oxidetalis/src/database/blacklist.rs
+++ b/crates/oxidetalis/src/database/blacklist.rs
@@ -20,7 +20,7 @@ use oxidetalis_core::types::PublicKey;
use oxidetalis_entities::prelude::*;
use sea_orm::DatabaseConnection;
-use crate::errors::ApiResult;
+use crate::errors::ServerResult;
/// Extension trait for the `DatabaseConnection` to work with the blacklist
/// table
@@ -31,7 +31,7 @@ pub trait BlackListExt {
&self,
blacklister: &UserModel,
target_public_key: &PublicKey,
- ) -> ApiResult;
+ ) -> ServerResult;
}
impl BlackListExt for DatabaseConnection {
@@ -40,7 +40,7 @@ impl BlackListExt for DatabaseConnection {
&self,
blacklister: &UserModel,
target_public_key: &PublicKey,
- ) -> ApiResult {
+ ) -> ServerResult {
blacklister
.find_related(BlacklistEntity)
.filter(BlacklistColumn::Target.eq(target_public_key.to_string()))
diff --git a/crates/oxidetalis/src/database/in_chat_requests.rs b/crates/oxidetalis/src/database/in_chat_requests.rs
index 137fee3..537b3fe 100644
--- a/crates/oxidetalis/src/database/in_chat_requests.rs
+++ b/crates/oxidetalis/src/database/in_chat_requests.rs
@@ -21,7 +21,7 @@ use oxidetalis_core::types::PublicKey;
use oxidetalis_entities::prelude::*;
use sea_orm::DatabaseConnection;
-use crate::websocket::errors::{WsError, WsResult};
+use crate::errors::ServerResult;
/// Extension trait for the `in_chat_requests` table.
pub trait InChatRequestsExt {
@@ -30,7 +30,7 @@ pub trait InChatRequestsExt {
&self,
requester: &PublicKey,
recipient: &UserModel,
- ) -> WsResult<()>;
+ ) -> ServerResult<()>;
}
impl InChatRequestsExt for DatabaseConnection {
@@ -39,16 +39,12 @@ impl InChatRequestsExt for DatabaseConnection {
&self,
sender: &PublicKey,
recipient: &UserModel,
- ) -> WsResult<()> {
- if sender.to_string() == recipient.public_key {
- return Err(WsError::CannotSendChatRequestToSelf);
- }
+ ) -> ServerResult<()> {
if recipient
.find_related(InChatRequestsEntity)
.filter(InChatRequestsColumn::Sender.eq(sender.to_string()))
.one(self)
- .await
- .map_err(|_| WsError::InternalServerError)?
+ .await?
.is_none()
{
InChatRequestsActiveModel {
@@ -58,8 +54,7 @@ impl InChatRequestsExt for DatabaseConnection {
..Default::default()
}
.save(self)
- .await
- .map_err(|_| WsError::InternalServerError)?;
+ .await?;
}
Ok(())
}
diff --git a/crates/oxidetalis/src/database/out_chat_requests.rs b/crates/oxidetalis/src/database/out_chat_requests.rs
index b687997..b51274d 100644
--- a/crates/oxidetalis/src/database/out_chat_requests.rs
+++ b/crates/oxidetalis/src/database/out_chat_requests.rs
@@ -21,10 +21,7 @@ use oxidetalis_core::types::PublicKey;
use oxidetalis_entities::prelude::*;
use sea_orm::DatabaseConnection;
-use crate::{
- errors::ApiResult,
- websocket::errors::{WsError, WsResult},
-};
+use crate::{errors::ServerResult, websocket::errors::WsError};
/// Extension trait for the `out_chat_requests` table.
pub trait OutChatRequestsExt {
@@ -33,14 +30,14 @@ pub trait OutChatRequestsExt {
&self,
requester: &UserModel,
recipient: &PublicKey,
- ) -> ApiResult;
+ ) -> ServerResult;
/// Save the chat request in the requester table
async fn save_out_chat_request(
&self,
requester: &UserModel,
recipient: &PublicKey,
- ) -> WsResult<()>;
+ ) -> ServerResult<()>;
}
impl OutChatRequestsExt for DatabaseConnection {
@@ -49,7 +46,7 @@ impl OutChatRequestsExt for DatabaseConnection {
&self,
requester: &UserModel,
recipient: &PublicKey,
- ) -> ApiResult {
+ ) -> ServerResult {
requester
.find_related(OutChatRequestsEntity)
.filter(OutChatRequestsColumn::Recipient.eq(recipient.to_string()))
@@ -64,13 +61,9 @@ impl OutChatRequestsExt for DatabaseConnection {
&self,
requester: &UserModel,
recipient: &PublicKey,
- ) -> WsResult<()> {
- if self
- .have_chat_request_to(requester, recipient)
- .await
- .map_err(|_| WsError::InternalServerError)?
- {
- return Err(WsError::AlreadySendChatRequest);
+ ) -> ServerResult<()> {
+ if self.have_chat_request_to(requester, recipient).await? {
+ return Err(WsError::AlreadySendChatRequest.into());
}
OutChatRequestsActiveModel {
sender_id: Set(requester.id),
@@ -79,8 +72,7 @@ impl OutChatRequestsExt for DatabaseConnection {
..Default::default()
}
.save(self)
- .await
- .map_err(|_| WsError::InternalServerError)?;
+ .await?;
Ok(())
}
}
diff --git a/crates/oxidetalis/src/database/user.rs b/crates/oxidetalis/src/database/user.rs
index 4cc27a0..4e94901 100644
--- a/crates/oxidetalis/src/database/user.rs
+++ b/crates/oxidetalis/src/database/user.rs
@@ -21,29 +21,29 @@ use oxidetalis_core::types::PublicKey;
use oxidetalis_entities::prelude::*;
use sea_orm::DatabaseConnection;
-use crate::errors::{ApiError, ApiResult};
+use crate::{errors::ServerResult, routes::ApiError};
pub trait UserTableExt {
/// Returns true if there is users in the database
- async fn users_exists_in_database(&self) -> ApiResult;
+ async fn users_exists_in_database(&self) -> ServerResult;
/// Register new user
- async fn register_user(&self, public_key: &PublicKey, is_admin: bool) -> ApiResult<()>;
+ async fn register_user(&self, public_key: &PublicKey, is_admin: bool) -> ServerResult<()>;
/// Returns user by its public key
- async fn get_user_by_pubk(&self, public_key: &PublicKey) -> ApiResult