feat: Websocket error macro

`ws_errors` macro to make adding new errors easy

Signed-off-by: Awiteb <a@4rs.nl>
This commit is contained in:
Awiteb 2024-07-10 20:23:29 +03:00
parent 033a21f733
commit b4b3b537fd
Signed by: awiteb
GPG key ID: 3F6B55640AA6682F
3 changed files with 67 additions and 35 deletions

View file

@ -0,0 +1,59 @@
// OxideTalis Messaging Protocol homeserver implementation
// Copyright (C) 2024 OxideTalis Developers <otmp@4rs.nl>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://gnu.org/licenses/agpl-3.0>.
//! OxideTalis server macros, to make the code more readable and easier to
//! write.
/// Macro to create the `WsError` enum with the given error names and reasons.
///
/// ## Example
/// ```rust,ignore
/// ws_errors! {
/// FirstError = "This is the first error",
/// SecondError = "This is the second error",
/// }
/// ```
#[macro_export]
macro_rules! ws_errors {
($($name:ident = $reason:tt),+ $(,)?) => {
#[derive(Debug)]
#[doc = "Websocket errors, returned in the websocket communication"]
pub enum WsError {
$(
#[doc = $reason]
$name
),+
}
impl WsError {
#[doc = "Returns error name"]
pub const fn name(&self) -> &'static str {
match self {
$(
WsError::$name => stringify!($name)
),+
}
}
#[doc = "Returns the error reason"]
pub const fn reason(&self) -> &'static str {
match self {
$(
WsError::$name => $reason
),+
}
}
}
};
}

View file

@ -26,6 +26,7 @@ use salvo::{conn::TcpListener, Listener, Server};
mod database;
mod errors;
mod extensions;
mod macros;
mod middlewares;
mod nonce;
mod routes;

View file

@ -16,42 +16,14 @@
//! Websocket errors
use crate::ws_errors;
/// Result type of websocket
pub type WsResult<T> = Result<T, WsError>;
/// Websocket errors, returned in the websocket communication
#[derive(Debug)]
pub enum WsError {
/// The signature is invalid
InvalidSignature,
/// Message type must be text
NotTextMessage,
/// Invalid json data
InvalidJsonData,
/// Unknown client event
UnknownClientEvent,
}
impl WsError {
/// Returns error name
pub const fn name(&self) -> &'static str {
match self {
WsError::InvalidSignature => "InvalidSignature",
WsError::NotTextMessage => "NotTextMessage",
WsError::InvalidJsonData => "InvalidJsonData",
WsError::UnknownClientEvent => "UnknownClientEvent",
}
}
/// Returns the error reason
pub const fn reason(&self) -> &'static str {
match self {
WsError::InvalidSignature => "Invalid event signature",
WsError::NotTextMessage => "The websocket message must be text message",
WsError::InvalidJsonData => "Received invalid json data, the text must be valid json",
WsError::UnknownClientEvent => {
"Unknown client event, the event is not recognized by the server"
}
}
}
ws_errors! {
InvalidSignature = "Invalid event signature",
NotTextMessage = "The websocket message must be text message",
InvalidJsonData = "Received invalid json data, the text must be valid json",
UnknownClientEvent = "Unknown client event, the event is not recognized by the server",
}