feat: Macro to try the websocket error

Signed-off-by: Awiteb <a@4rs.nl>
This commit is contained in:
Awiteb 2024-07-12 22:48:54 +03:00
parent 226df1961b
commit b72fb8e7f6
Signed by: awiteb
GPG key ID: 3F6B55640AA6682F

View file

@ -17,6 +17,37 @@
//! OxideTalis server macros, to make the code more readable and easier to //! OxideTalis server macros, to make the code more readable and easier to
//! write. //! write.
/// Macro to return a [`ServerEvent`] with a [`WsError::InternalServerError`] if
/// the result of an expression is an [`Err`].
///
/// ## Example
/// ```rust,ignore
/// fn example() -> ServerEvent {
/// // some_function() returns a Result, if it's an Err, return an
/// // ServerEvent::InternalServerError
/// let result = try_ws!(some_function());
/// ServerEvent::from(result)
/// }
/// ```
///
/// [`ServerEvent`]: crate::websocket::ServerEvent
/// [`WsError::InternalServerError`]: crate::websocket::errors::WsError::InternalServerError
/// [`Err`]: std::result::Result::Err
#[macro_export]
macro_rules! try_ws {
($result_expr:expr) => {
match $result_expr {
Ok(val) => val,
Err(err) => {
log::error!("Error in try_ws macro: {err:?}");
return $crate::websocket::ServerEvent::<$crate::websocket::Unsigned>::from(
$crate::websocket::errors::WsError::InternalServerError,
);
}
}
};
}
/// Macro to create the `WsError` enum with the given error names and reasons. /// Macro to create the `WsError` enum with the given error names and reasons.
/// ///
/// ## Example /// ## Example