1#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)]
3#[non_exhaustive]
4pub enum ConnectError {
5 #[error("unauthorized")]
8 Unauthorized,
9
10 #[error("forbidden")]
13 Forbidden,
14}
15
16impl ConnectError {
17 #[cfg(any(feature = "noq", feature = "quinn", feature = "quiche", feature = "websocket"))]
20 pub(crate) fn from_status_u16(status: u16) -> Option<Self> {
21 match status {
22 401 => Some(Self::Unauthorized),
23 403 => Some(Self::Forbidden),
24 _ => None,
25 }
26 }
27
28 pub fn is_auth(&self) -> bool {
31 matches!(self, Self::Unauthorized | Self::Forbidden)
32 }
33}
34
35#[cfg(all(
36 test,
37 any(feature = "noq", feature = "quinn", feature = "quiche", feature = "websocket")
38))]
39mod tests {
40 use super::*;
41
42 #[test]
43 fn auth_statuses_are_terminal() {
44 assert_eq!(ConnectError::from_status_u16(401), Some(ConnectError::Unauthorized));
45 assert_eq!(ConnectError::from_status_u16(403), Some(ConnectError::Forbidden));
46 }
47
48 #[test]
49 fn non_auth_statuses_are_not_terminal() {
50 for status in [400, 404, 500] {
51 assert_eq!(ConnectError::from_status_u16(status), None);
52 }
53 }
54}