Skip to main content

webtrans_proto/
error.rs

1//! Error mapping and error types for WebTransport protocol handling.
2
3// WebTransport shares the HTTP/3 error space, so the code range must be offset.
4const ERROR_FIRST: u64 = 0x52e4a40fa8db;
5const ERROR_LAST: u64 = 0x52e5ac983162;
6
7/// Map an HTTP/3 application error code into WebTransport error space.
8pub const fn error_from_http3(code: u64) -> Option<u32> {
9    if code < ERROR_FIRST || code > ERROR_LAST {
10        return None;
11    }
12
13    let code = code - ERROR_FIRST;
14    let code = code - code / 0x1f;
15
16    Some(code as u32)
17}
18
19/// Map a WebTransport application error code into the reserved HTTP/3 space.
20pub const fn error_to_http3(code: u32) -> u64 {
21    ERROR_FIRST + code as u64 + code as u64 / 0x1e
22}
23
24use thiserror::Error;
25
26/// Convenience result type for WebTransport protocol and transport operations.
27pub type Result<T> = std::result::Result<T, Error>;
28
29/// Error categories that can occur when using WebTransport.
30#[derive(Debug, Error)]
31pub enum Error {
32    /// Transport connection is closed.
33    #[error("connection closed")]
34    Closed,
35
36    /// URL parsing or validation failed.
37    #[error("invalid url: {0}")]
38    InvalidUrl(String),
39
40    /// Protocol-level constraint was violated.
41    #[error("protocol error: {0}")]
42    Protocol(String),
43
44    /// Generic I/O failure.
45    #[error("io error: {0}")]
46    Io(String),
47
48    /// TLS configuration or handshake failure.
49    #[error("tls error: {0}")]
50    Tls(String),
51
52    /// Requested feature is not supported by this implementation.
53    #[error("unsupported: {0}")]
54    Unsupported(String),
55
56    /// WebTransport and HTTP/3 semantic errors reported by the protocol.
57    #[error("session error: {0}")]
58    Session(String),
59
60    /// Catch-all error variant for miscellaneous failures.
61    #[error("other error: {0}")]
62    Other(String),
63}
64
65impl Error {
66    /// Wrap a displayable error value in `Error::Other`.
67    pub fn other<E: std::fmt::Display>(e: E) -> Self {
68        Self::Other(e.to_string())
69    }
70}