Skip to main content

ws_kit/
error.rs

1//! Error types for `ws-kit`.
2//!
3//! All WebSocket failures are represented by [`WsError`]. For token-extraction
4//! specific errors see [`crate::extractor::WsAuthError`].
5
6use thiserror::Error;
7
8/// WebSocket error variants.
9///
10/// Each variant maps to a failure mode encountered during authentication,
11/// connection management, or message handling.
12#[derive(Debug, Error, Clone, PartialEq, Eq)]
13pub enum WsError {
14    /// No authentication token was provided.
15    #[error("authentication token missing")]
16    AuthMissing,
17
18    /// Provided authentication token is invalid or expired.
19    #[error("authentication token invalid")]
20    AuthInvalid,
21
22    /// Connection limit reached.
23    #[error("too many connections")]
24    TooManyConnections,
25
26    /// Broadcast channel is full or lagged.
27    #[error("broadcast channel full")]
28    BroadcastFull,
29
30    /// Incoming message could not be parsed or is semantically invalid.
31    #[error("invalid message")]
32    InvalidMessage,
33
34    /// Connection has been closed.
35    #[error("connection closed")]
36    Closed,
37}
38
39#[cfg(test)]
40mod tests {
41    use super::*;
42
43    #[test]
44    fn display_messages() {
45        assert_eq!(
46            WsError::AuthMissing.to_string(),
47            "authentication token missing"
48        );
49        assert_eq!(
50            WsError::AuthInvalid.to_string(),
51            "authentication token invalid"
52        );
53        assert_eq!(
54            WsError::TooManyConnections.to_string(),
55            "too many connections"
56        );
57        assert_eq!(WsError::BroadcastFull.to_string(), "broadcast channel full");
58        assert_eq!(WsError::InvalidMessage.to_string(), "invalid message");
59        assert_eq!(WsError::Closed.to_string(), "connection closed");
60    }
61
62    #[test]
63    fn clone_eq() {
64        let e = WsError::AuthMissing;
65        assert_eq!(e.clone(), WsError::AuthMissing);
66        assert_ne!(WsError::AuthMissing, WsError::Closed);
67    }
68}