Skip to main content

simoom_quic/
error.rs

1//! Transport error codes (RFC 9000 ยง20).
2
3use std::fmt;
4
5pub const NO_ERROR: u64 = 0x0;
6pub const INTERNAL_ERROR: u64 = 0x1;
7pub const CONNECTION_REFUSED: u64 = 0x2;
8pub const FLOW_CONTROL_ERROR: u64 = 0x3;
9pub const STREAM_LIMIT_ERROR: u64 = 0x4;
10pub const STREAM_STATE_ERROR: u64 = 0x5;
11pub const FINAL_SIZE_ERROR: u64 = 0x6;
12pub const FRAME_ENCODING_ERROR: u64 = 0x7;
13pub const TRANSPORT_PARAMETER_ERROR: u64 = 0x8;
14pub const CONNECTION_ID_LIMIT_ERROR: u64 = 0x9;
15pub const PROTOCOL_VIOLATION: u64 = 0xa;
16pub const INVALID_TOKEN: u64 = 0xb;
17pub const APPLICATION_ERROR: u64 = 0xc;
18pub const CRYPTO_BUFFER_EXCEEDED: u64 = 0xd;
19pub const KEY_UPDATE_ERROR: u64 = 0xe;
20pub const AEAD_LIMIT_REACHED: u64 = 0xf;
21pub const NO_VIABLE_PATH: u64 = 0x10;
22/// `CRYPTO_ERROR` base: the TLS alert is added to it.
23pub const CRYPTO_ERROR: u64 = 0x100;
24
25/// A connection error: the code, the frame type that caused it (if any) and a reason.
26#[derive(Clone, Debug, PartialEq, Eq)]
27pub struct TransportError {
28    pub code: u64,
29    pub frame_type: u64,
30    pub reason: &'static str,
31}
32
33impl TransportError {
34    pub const fn new(code: u64, reason: &'static str) -> Self {
35        TransportError { code, frame_type: 0, reason }
36    }
37    pub const fn frame(code: u64, frame_type: u64, reason: &'static str) -> Self {
38        TransportError { code, frame_type, reason }
39    }
40    pub const fn proto(reason: &'static str) -> Self {
41        Self::new(PROTOCOL_VIOLATION, reason)
42    }
43    pub const fn encoding(reason: &'static str) -> Self {
44        Self::new(FRAME_ENCODING_ERROR, reason)
45    }
46}
47
48impl fmt::Display for TransportError {
49    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50        write!(f, "transport error 0x{:x}: {}", self.code, self.reason)
51    }
52}
53
54impl std::error::Error for TransportError {}
55
56/// Errors returned by the stream API.
57#[derive(Clone, Copy, Debug, PartialEq, Eq)]
58pub enum StreamError {
59    /// The stream does not exist or was already closed.
60    UnknownStream,
61    /// The operation does not apply to this stream's direction.
62    InvalidDirection,
63    /// Flow control or the send buffer is full; wait for `Event::StreamWritable`.
64    Blocked,
65    /// No data available yet; wait for `Event::StreamReadable`.
66    WouldBlock,
67    /// The peer sent STOP_SENDING with this code.
68    Stopped(u64),
69    /// The peer reset the stream with this code.
70    Reset(u64),
71    /// The send side was already finished.
72    Finished,
73    /// The peer's stream limit is reached; wait for `Event::StreamsAvailable`.
74    LimitReached,
75    /// The connection is closed.
76    ConnectionClosed,
77}
78
79impl fmt::Display for StreamError {
80    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
81        fmt::Debug::fmt(self, f)
82    }
83}
84
85impl std::error::Error for StreamError {}