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:
parent
033a21f733
commit
b4b3b537fd
3 changed files with 67 additions and 35 deletions
59
crates/oxidetalis/src/macros.rs
Normal file
59
crates/oxidetalis/src/macros.rs
Normal 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
|
||||||
|
),+
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
|
@ -26,6 +26,7 @@ use salvo::{conn::TcpListener, Listener, Server};
|
||||||
mod database;
|
mod database;
|
||||||
mod errors;
|
mod errors;
|
||||||
mod extensions;
|
mod extensions;
|
||||||
|
mod macros;
|
||||||
mod middlewares;
|
mod middlewares;
|
||||||
mod nonce;
|
mod nonce;
|
||||||
mod routes;
|
mod routes;
|
||||||
|
|
|
@ -16,42 +16,14 @@
|
||||||
|
|
||||||
//! Websocket errors
|
//! Websocket errors
|
||||||
|
|
||||||
|
use crate::ws_errors;
|
||||||
|
|
||||||
/// Result type of websocket
|
/// Result type of websocket
|
||||||
pub type WsResult<T> = Result<T, WsError>;
|
pub type WsResult<T> = Result<T, WsError>;
|
||||||
|
|
||||||
/// Websocket errors, returned in the websocket communication
|
ws_errors! {
|
||||||
#[derive(Debug)]
|
InvalidSignature = "Invalid event signature",
|
||||||
pub enum WsError {
|
NotTextMessage = "The websocket message must be text message",
|
||||||
/// The signature is invalid
|
InvalidJsonData = "Received invalid json data, the text must be valid json",
|
||||||
InvalidSignature,
|
UnknownClientEvent = "Unknown client event, the event is not recognized by the server",
|
||||||
/// 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"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue