Skip to main content

krot_proto/
error.rs

1//! Application-level error codes (§8).
2//!
3//! Represented as a `u16` on the wire so that unknown values decode into
4//! [`ErrorCode`] rather than causing a hard parse failure. This preserves
5//! forward compatibility when future revisions introduce new codes.
6
7use serde::{Deserialize, Serialize};
8
9/// Numeric error code. See §8 for the full table.
10#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
11#[serde(transparent)]
12pub struct ErrorCode(pub u16);
13
14impl ErrorCode {
15    // Generic
16    pub const NO_ERROR: Self = Self(0x0000);
17    pub const PROTOCOL_VIOLATION: Self = Self(0x0001);
18    pub const FRAME_TOO_LARGE: Self = Self(0x0002);
19    pub const UNKNOWN_STREAM_KIND: Self = Self(0x0003);
20    pub const UNEXPECTED_STREAM: Self = Self(0x0004);
21
22    // Authentication
23    pub const AUTHENTICATION_FAILED: Self = Self(0x0010);
24    pub const UNKNOWN_IDENTITY: Self = Self(0x0011);
25    pub const KEY_REVOKED: Self = Self(0x0012);
26    pub const TOKEN_EXPIRED: Self = Self(0x0013);
27    pub const ENROLL_DISABLED: Self = Self(0x0014);
28
29    // Tunnel registration
30    pub const LABEL_UNAVAILABLE: Self = Self(0x0020);
31    pub const LABEL_FORBIDDEN: Self = Self(0x0021);
32    pub const LABEL_INVALID: Self = Self(0x0022);
33    pub const TUNNEL_LIMIT_EXCEEDED: Self = Self(0x0023);
34    pub const HTTP_NOT_AVAILABLE: Self = Self(0x0024);
35    pub const PORT_POOL_EXHAUSTED: Self = Self(0x0025);
36
37    // Quota
38    pub const RATE_LIMITED: Self = Self(0x0030);
39    pub const QUOTA_EXCEEDED: Self = Self(0x0031);
40
41    // Session resume
42    pub const RESUME_UNKNOWN: Self = Self(0x0040);
43    pub const RESUME_IDENTITY_MISMATCH: Self = Self(0x0041);
44
45    // Server lifecycle
46    pub const SERVER_SHUTDOWN: Self = Self(0x00F0);
47    pub const INTERNAL_ERROR: Self = Self(0x00F1);
48
49    #[inline]
50    pub const fn raw(self) -> u16 {
51        self.0
52    }
53}
54
55impl core::fmt::Display for ErrorCode {
56    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
57        let name = match *self {
58            Self::NO_ERROR => "NoError",
59            Self::PROTOCOL_VIOLATION => "ProtocolViolation",
60            Self::FRAME_TOO_LARGE => "FrameTooLarge",
61            Self::UNKNOWN_STREAM_KIND => "UnknownStreamKind",
62            Self::UNEXPECTED_STREAM => "UnexpectedStream",
63            Self::AUTHENTICATION_FAILED => "AuthenticationFailed",
64            Self::UNKNOWN_IDENTITY => "UnknownIdentity",
65            Self::KEY_REVOKED => "KeyRevoked",
66            Self::TOKEN_EXPIRED => "TokenExpired",
67            Self::ENROLL_DISABLED => "EnrollDisabled",
68            Self::LABEL_UNAVAILABLE => "LabelUnavailable",
69            Self::LABEL_FORBIDDEN => "LabelForbidden",
70            Self::LABEL_INVALID => "LabelInvalid",
71            Self::TUNNEL_LIMIT_EXCEEDED => "TunnelLimitExceeded",
72            Self::HTTP_NOT_AVAILABLE => "HttpNotAvailable",
73            Self::PORT_POOL_EXHAUSTED => "PortPoolExhausted",
74            Self::RATE_LIMITED => "RateLimited",
75            Self::QUOTA_EXCEEDED => "QuotaExceeded",
76            Self::RESUME_UNKNOWN => "ResumeUnknown",
77            Self::RESUME_IDENTITY_MISMATCH => "ResumeIdentityMismatch",
78            Self::SERVER_SHUTDOWN => "ServerShutdown",
79            Self::INTERNAL_ERROR => "InternalError",
80            _ => return write!(f, "ErrorCode(0x{:04X})", self.0),
81        };
82        write!(f, "{name}")
83    }
84}