1use std::io;
2
3use thiserror::Error;
4
5use crate::frame::{
6 ErrorCode,
7 Length,
8 StreamID,
9};
10
11#[repr(u32)]
13#[derive(Copy, Clone, Debug, Eq, PartialEq, Error)]
14#[allow(missing_docs)]
15pub enum Error {
16 #[error("No Error")]
17 None = 0x00,
18 #[error("Protocol Error")]
19 Protocol = 0x01,
20 #[error("Internal Error")]
21 Internal = 0x02,
22 #[error("Flow Control Error")]
23 FlowControl = 0x03,
24 #[error("Stream Closed")]
25 StreamClosed = 0x04,
26 #[error("Stream Refused")]
27 StreamRefused = 0x05,
28 #[error("Stream Cancelled")]
29 StreamCancelled = 0x06,
30 #[error("Stream Reset")]
31 StreamReset = 0x07,
32 #[error("Frame Size Error")]
33 FrameSizeError = 0x08,
34 #[error("Accept Queue Full")]
35 AcceptQueueFull = 0x09,
36 #[error("Enhance Your Calm")]
37 EnhanceYourCalm = 0x0A,
38 #[error("Remote Gone Away")]
39 RemoteGoneAway = 0x0B,
40 #[error("Streams Exhausted")]
41 StreamsExhausted = 0x0C,
42 #[error("Write Timeout")]
43 WriteTimeout = 0x0D,
44 #[error("Session Closed")]
45 SessionClosed = 0x0E,
46 #[error("Peer EOF")]
47 PeerEOF = 0x0F,
48
49 #[error("Unknown Error")]
50 ErrorUnknown = u32::MAX,
51}
52
53impl From<Error> for ErrorCode {
54 fn from(other: Error) -> ErrorCode {
55 ErrorCode::mask(other as u32)
56 }
57}
58
59impl From<ErrorCode> for Error {
60 fn from(other: ErrorCode) -> Error {
61 use Error::*;
62 match *other {
63 0x00 => None,
64 0x01 => Protocol,
65 0x02 => Internal,
66 0x03 => FlowControl,
67 0x04 => StreamClosed,
68 0x05 => StreamRefused,
69 0x06 => StreamCancelled,
70 0x07 => StreamReset,
71 0x08 => FrameSizeError,
72 0x09 => AcceptQueueFull,
73 0x0A => EnhanceYourCalm,
74 0x0B => RemoteGoneAway,
75 0x0C => StreamsExhausted,
76 0x0D => WriteTimeout,
77 0x0E => SessionClosed,
78 0x0F => PeerEOF,
79
80 _ => ErrorUnknown,
81 }
82 }
83}
84
85#[derive(Copy, Clone, Debug, Error, PartialEq, Eq)]
86pub enum InvalidHeader {
87 #[error("StreamID should be non-zero")]
88 ZeroStreamID,
89 #[error("StreamID should be zero, got {0}")]
90 NonZeroStreamID(StreamID),
91 #[error("Length should be {expected}, got {actual}")]
92 Length { expected: Length, actual: Length },
93 #[error("Length should be at least {expected}, got {actual}")]
94 MinLength { expected: Length, actual: Length },
95 #[error("Invalid frame type: {0}")]
96 Type(u8),
97}
98
99impl From<InvalidHeader> for io::Error {
100 fn from(other: InvalidHeader) -> io::Error {
101 io::Error::other(other)
102 }
103}
104
105impl From<InvalidHeader> for Error {
106 fn from(_: InvalidHeader) -> Error {
107 Error::Protocol
108 }
109}