feat: Macro to try the websocket error
Signed-off-by: Awiteb <a@4rs.nl>
This commit is contained in:
parent
226df1961b
commit
b72fb8e7f6
1 changed files with 31 additions and 0 deletions
|
@ -17,6 +17,37 @@
|
|||
//! OxideTalis server macros, to make the code more readable and easier to
|
||||
//! 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.
|
||||
///
|
||||
/// ## Example
|
||||
|
|
Loading…
Reference in a new issue