Refactor for global error type
This commit is contained in:
@@ -18,3 +18,4 @@ hex = "0.4.3"
|
|||||||
rand = "0.8.5"
|
rand = "0.8.5"
|
||||||
mail-send = "0.4.8"
|
mail-send = "0.4.8"
|
||||||
regex = "1.10.5"
|
regex = "1.10.5"
|
||||||
|
realm_shared = { path = "../shared" }
|
||||||
|
|||||||
@@ -11,8 +11,9 @@ use sha3::digest::Update;
|
|||||||
use sqlx::{MySql, Pool, Row};
|
use sqlx::{MySql, Pool, Row};
|
||||||
use tarpc::context::Context;
|
use tarpc::context::Context;
|
||||||
|
|
||||||
use crate::types::{AuthEmail, AuthUser, ErrorCode, RealmAuth};
|
use crate::types::{AuthEmail, AuthUser, RealmAuth};
|
||||||
use crate::types::ErrorCode::*;
|
use realm_shared::types::ErrorCode;
|
||||||
|
use realm_shared::types::ErrorCode::*;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct RealmAuthServer {
|
pub struct RealmAuthServer {
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
use realm_shared::types::ErrorCode;
|
||||||
|
|
||||||
#[tarpc::service]
|
#[tarpc::service]
|
||||||
pub trait RealmAuth {
|
pub trait RealmAuth {
|
||||||
@@ -21,21 +22,6 @@ pub trait RealmAuth {
|
|||||||
// TODO: OAuth login, check against email, store token, take avatar: Google, Apple, GitHub, Discord
|
// 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)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
pub struct AuthUser {
|
pub struct AuthUser {
|
||||||
pub id: u32,
|
pub id: u32,
|
||||||
|
|||||||
@@ -15,3 +15,4 @@ chrono = { version = "0.4.24", features = ["serde"] }
|
|||||||
sqlx = { version = "0.7", features = [ "runtime-tokio", "tls-rustls", "mysql", "chrono" ] }
|
sqlx = { version = "0.7", features = [ "runtime-tokio", "tls-rustls", "mysql", "chrono" ] }
|
||||||
dotenvy = "0.15"
|
dotenvy = "0.15"
|
||||||
realm_auth = { path = "../auth" }
|
realm_auth = { path = "../auth" }
|
||||||
|
realm_shared = { path = "../shared" }
|
||||||
@@ -3,8 +3,9 @@ use chrono::{DateTime, Utc};
|
|||||||
use sqlx::{Error, MySql, Pool, Row};
|
use sqlx::{Error, MySql, Pool, Row};
|
||||||
use sqlx::mysql::MySqlRow;
|
use sqlx::mysql::MySqlRow;
|
||||||
use tarpc::context::Context;
|
use tarpc::context::Context;
|
||||||
use crate::types::{Edit, ErrorCode, Message, MessageData, Reaction, RealmChat, Redaction, Reply, Room, User};
|
use crate::types::{Edit, Message, MessageData, Reaction, RealmChat, Redaction, Reply, Room, User};
|
||||||
use crate::types::ErrorCode::*;
|
use realm_shared::types::ErrorCode::*;
|
||||||
|
use realm_shared::types::ErrorCode;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct RealmChatServer {
|
pub struct RealmChatServer {
|
||||||
@@ -84,7 +85,7 @@ impl RealmChat for RealmChatServer {
|
|||||||
self.dbmessage_to_message(row)
|
self.dbmessage_to_message(row)
|
||||||
},
|
},
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
Err(NotFound)
|
Err(MessageNotFound)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -122,7 +123,7 @@ impl RealmChat for RealmChatServer {
|
|||||||
|
|
||||||
match result {
|
match result {
|
||||||
Ok(row) => { self.dbroom_to_room(row) },
|
Ok(row) => { self.dbroom_to_room(row) },
|
||||||
Err(_) => Err(NotFound),
|
Err(_) => Err(RoomNotFound),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -131,7 +132,7 @@ impl RealmChat for RealmChatServer {
|
|||||||
|
|
||||||
match result {
|
match result {
|
||||||
Ok(row) => { self.dbuser_to_user(row) },
|
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");
|
let admin_only_view: Result<bool, _> = row.try_get("admin_only_view");
|
||||||
|
|
||||||
if id.is_err() {
|
if id.is_err() {
|
||||||
return Err(FailedToUnwrapDB)
|
return Err(MalformedDBResponse)
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(Room {
|
Ok(Room {
|
||||||
@@ -200,7 +201,7 @@ impl RealmChatServer {
|
|||||||
let admin: Result<bool, _> = row.try_get("admin");
|
let admin: Result<bool, _> = row.try_get("admin");
|
||||||
|
|
||||||
if id.is_err() {
|
if id.is_err() {
|
||||||
return Err(FailedToUnwrapDB)
|
return Err(MalformedDBResponse)
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(User {
|
Ok(User {
|
||||||
@@ -220,7 +221,7 @@ impl RealmChatServer {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if type_enum == "" {
|
if type_enum == "" {
|
||||||
return Err(FailedToUnwrapDB)
|
return Err(MalformedDBResponse)
|
||||||
}
|
}
|
||||||
|
|
||||||
let id: u32 = row.try_get("message.id").unwrap();
|
let id: u32 = row.try_get("message.id").unwrap();
|
||||||
@@ -295,7 +296,7 @@ impl RealmChatServer {
|
|||||||
}),
|
}),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
_ => { Err(FailedToUnwrapDB) }
|
_ => { Err(MalformedDBResponse) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
use chrono::{DateTime, TimeZone, Utc};
|
use chrono::{DateTime, TimeZone, Utc};
|
||||||
use tarpc::serde::{Deserialize, Serialize};
|
use tarpc::serde::{Deserialize, Serialize};
|
||||||
|
use realm_shared::types::ErrorCode;
|
||||||
|
|
||||||
#[tarpc::service]
|
#[tarpc::service]
|
||||||
pub trait RealmChat {
|
pub trait RealmChat {
|
||||||
@@ -28,15 +29,6 @@ pub trait RealmChat {
|
|||||||
// unban user
|
// unban user
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
||||||
pub enum ErrorCode {
|
|
||||||
None,
|
|
||||||
Error,
|
|
||||||
Unauthorized,
|
|
||||||
NotFound,
|
|
||||||
FailedToUnwrapDB,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
pub struct Message {
|
pub struct Message {
|
||||||
pub id: u32,
|
pub id: u32,
|
||||||
|
|||||||
7
shared/Cargo.toml
Normal file
7
shared/Cargo.toml
Normal 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
1
shared/src/lib.rs
Normal file
@@ -0,0 +1 @@
|
|||||||
|
pub mod types;
|
||||||
21
shared/src/types.rs
Normal file
21
shared/src/types.rs
Normal 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,
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user