Skip to main content

moq_native/
connect.rs

1/// Error returned when connection setup fails for a terminal auth reason.
2#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)]
3#[non_exhaustive]
4pub enum ConnectError {
5	/// The server rejected the credentials (HTTP 401). Retrying with the same
6	/// token will fail again.
7	#[error("unauthorized")]
8	Unauthorized,
9
10	/// The credentials were understood but don't grant access to this path
11	/// (HTTP 403).
12	#[error("forbidden")]
13	Forbidden,
14}
15
16impl ConnectError {
17	pub(crate) fn from_status_u16(status: u16) -> Option<Self> {
18		match status {
19			401 => Some(Self::Unauthorized),
20			403 => Some(Self::Forbidden),
21			_ => None,
22		}
23	}
24
25	/// Whether this is an authentication failure, meaning a retry is pointless
26	/// until the credentials change.
27	pub fn is_auth(&self) -> bool {
28		matches!(self, Self::Unauthorized | Self::Forbidden)
29	}
30}
31
32#[cfg(test)]
33mod tests {
34	use super::*;
35
36	#[test]
37	fn auth_statuses_are_terminal() {
38		assert_eq!(ConnectError::from_status_u16(401), Some(ConnectError::Unauthorized));
39		assert_eq!(ConnectError::from_status_u16(403), Some(ConnectError::Forbidden));
40	}
41
42	#[test]
43	fn non_auth_statuses_are_not_terminal() {
44		for status in [400, 404, 500] {
45			assert_eq!(ConnectError::from_status_u16(status), None);
46		}
47	}
48}