Refactor for global error type

This commit is contained in:
2024-07-22 18:36:48 -04:00
Unverified
parent 4bd3112816
commit cb63e7d538
9 changed files with 48 additions and 37 deletions

View File

@@ -18,3 +18,4 @@ hex = "0.4.3"
rand = "0.8.5"
mail-send = "0.4.8"
regex = "1.10.5"
realm_shared = { path = "../shared" }

View File

@@ -11,8 +11,9 @@ use sha3::digest::Update;
use sqlx::{MySql, Pool, Row};
use tarpc::context::Context;
use crate::types::{AuthEmail, AuthUser, ErrorCode, RealmAuth};
use crate::types::ErrorCode::*;
use crate::types::{AuthEmail, AuthUser, RealmAuth};
use realm_shared::types::ErrorCode;
use realm_shared::types::ErrorCode::*;
#[derive(Clone)]
pub struct RealmAuthServer {

View File

@@ -1,4 +1,5 @@
use serde::{Deserialize, Serialize};
use realm_shared::types::ErrorCode;
#[tarpc::service]
pub trait RealmAuth {
@@ -21,21 +22,6 @@ pub trait RealmAuth {
// TODO: OAuth login, check against email, store token, take avatar: Google, Apple, GitHub, Discord
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum ErrorCode {
Error,
Unauthorized,
EmailTaken,
UsernameTaken,
InvalidLoginCode,
InvalidImage,
InvalidUsername,
InvalidEmail,
InvalidToken,
UnableToConnectToMail,
UnableToSendMail,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuthUser {
pub id: u32,

View File

@@ -15,3 +15,4 @@ chrono = { version = "0.4.24", features = ["serde"] }
sqlx = { version = "0.7", features = [ "runtime-tokio", "tls-rustls", "mysql", "chrono" ] }
dotenvy = "0.15"
realm_auth = { path = "../auth" }
realm_shared = { path = "../shared" }

View File

@@ -3,8 +3,9 @@ use chrono::{DateTime, Utc};
use sqlx::{Error, MySql, Pool, Row};
use sqlx::mysql::MySqlRow;
use tarpc::context::Context;
use crate::types::{Edit, ErrorCode, Message, MessageData, Reaction, RealmChat, Redaction, Reply, Room, User};
use crate::types::ErrorCode::*;
use crate::types::{Edit, Message, MessageData, Reaction, RealmChat, Redaction, Reply, Room, User};
use realm_shared::types::ErrorCode::*;
use realm_shared::types::ErrorCode;
#[derive(Clone)]
pub struct RealmChatServer {
@@ -84,7 +85,7 @@ impl RealmChat for RealmChatServer {
self.dbmessage_to_message(row)
},
Err(_) => {
Err(NotFound)
Err(MessageNotFound)
},
}
}
@@ -122,7 +123,7 @@ impl RealmChat for RealmChatServer {
match result {
Ok(row) => { self.dbroom_to_room(row) },
Err(_) => Err(NotFound),
Err(_) => Err(RoomNotFound),
}
}
@@ -131,7 +132,7 @@ impl RealmChat for RealmChatServer {
match result {
Ok(row) => { self.dbuser_to_user(row) },
Err(_) => Err(NotFound),
Err(_) => Err(UserNotFound),
}
}
@@ -180,7 +181,7 @@ impl RealmChatServer {
let admin_only_view: Result<bool, _> = row.try_get("admin_only_view");
if id.is_err() {
return Err(FailedToUnwrapDB)
return Err(MalformedDBResponse)
}
Ok(Room {
@@ -200,7 +201,7 @@ impl RealmChatServer {
let admin: Result<bool, _> = row.try_get("admin");
if id.is_err() {
return Err(FailedToUnwrapDB)
return Err(MalformedDBResponse)
}
Ok(User {
@@ -220,7 +221,7 @@ impl RealmChatServer {
};
if type_enum == "" {
return Err(FailedToUnwrapDB)
return Err(MalformedDBResponse)
}
let id: u32 = row.try_get("message.id").unwrap();
@@ -295,7 +296,7 @@ impl RealmChatServer {
}),
})
}
_ => { Err(FailedToUnwrapDB) }
_ => { Err(MalformedDBResponse) }
}
}
}

View File

@@ -1,5 +1,6 @@
use chrono::{DateTime, TimeZone, Utc};
use tarpc::serde::{Deserialize, Serialize};
use realm_shared::types::ErrorCode;
#[tarpc::service]
pub trait RealmChat {
@@ -28,15 +29,6 @@ pub trait RealmChat {
// unban user
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ErrorCode {
None,
Error,
Unauthorized,
NotFound,
FailedToUnwrapDB,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Message {
pub id: u32,

7
shared/Cargo.toml Normal file
View File

@@ -0,0 +1,7 @@
[package]
name = "realm_shared"
version = "0.1.0"
edition = "2021"
[dependencies]
serde = { version = "1.0.203", features = ["derive"] }

1
shared/src/lib.rs Normal file
View File

@@ -0,0 +1 @@
pub mod types;

21
shared/src/types.rs Normal file
View File

@@ -0,0 +1,21 @@
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum ErrorCode {
Error,
Unauthorized,
EmailTaken,
UsernameTaken,
InvalidLoginCode,
InvalidImage,
InvalidUsername,
InvalidEmail,
InvalidToken,
UnableToConnectToMail,
UnableToSendMail,
MessageNotFound,
RoomNotFound,
UserNotFound,
MalformedDBResponse,
}