websoc_kit/
error.rs

1use std::fmt::{Debug, Formatter};
2use std::{error, fmt};
3
4use tokio_tungstenite::tungstenite;
5
6use crate::connection_id::ConnectionId;
7
8pub type WebsocKitResult<T> = Result<T, WebsocKitError>;
9
10#[expect(clippy::module_name_repetitions)]
11#[derive(Debug)]
12pub enum WebsocKitError {
13    TungsteniteError(tungstenite::Error),
14
15    ConnectionDoesNotExist(ConnectionId),
16    TextMessagesNotAllowed(ConnectionId, String),
17}
18
19impl error::Error for WebsocKitError {}
20
21impl fmt::Display for WebsocKitError {
22    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
23        match self {
24            WebsocKitError::TungsteniteError(error) => write!(f, "Tungstenite error: '{error}'"),
25
26            WebsocKitError::ConnectionDoesNotExist(connection_id) => {
27                write!(
28                    f,
29                    "No websocket connection exists with id: '{connection_id}'"
30                )
31            }
32            WebsocKitError::TextMessagesNotAllowed(connection_id, invalid_text_message) => {
33                write!(
34                    f,
35                    "Terminated websocket '{connection_id}' because it sent a non-binary text message: '{invalid_text_message}'"
36                )
37            }
38        }
39    }
40}
41
42impl From<tungstenite::Error> for WebsocKitError {
43    fn from(error: tungstenite::Error) -> Self {
44        WebsocKitError::TungsteniteError(error)
45    }
46}