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// Tests exercise failure paths and invariants directly; unwrap/expect,
40// slicing, and panicking asserts are acceptable here — violations
41// surface as test failures, not production panics.
42#[allow(
43    clippy::unwrap_used,
44    clippy::expect_used,
45    clippy::indexing_slicing,
46    clippy::panic
47)]
48#[cfg(test)]
49mod tests {
50    use super::*;
51
52    #[test]
53    fn display_messages() {
54        assert_eq!(
55            WsError::AuthMissing.to_string(),
56            "authentication token missing"
57        );
58        assert_eq!(
59            WsError::AuthInvalid.to_string(),
60            "authentication token invalid"
61        );
62        assert_eq!(
63            WsError::TooManyConnections.to_string(),
64            "too many connections"
65        );
66        assert_eq!(WsError::BroadcastFull.to_string(), "broadcast channel full");
67        assert_eq!(WsError::InvalidMessage.to_string(), "invalid message");
68        assert_eq!(WsError::Closed.to_string(), "connection closed");
69    }
70
71    #[test]
72    fn clone_eq() {
73        let e = WsError::AuthMissing;
74        assert_eq!(e.clone(), WsError::AuthMissing);
75        assert_ne!(WsError::AuthMissing, WsError::Closed);
76    }
77}