1 use crate::frame::{Reason, StreamId};
2 
3 use std::{error, fmt, io};
4 
5 /// Errors that are received
6 #[derive(Debug)]
7 pub enum RecvError {
8     Connection(Reason),
9     Stream { id: StreamId, reason: Reason },
10     Io(io::Error),
11 }
12 
13 /// Errors caused by sending a message
14 #[derive(Debug)]
15 pub enum SendError {
16     /// User error
17     User(UserError),
18 
19     /// Connection error prevents sending.
20     Connection(Reason),
21 
22     /// I/O error
23     Io(io::Error),
24 }
25 
26 /// Errors caused by users of the library
27 #[derive(Debug)]
28 pub enum UserError {
29     /// The stream ID is no longer accepting frames.
30     InactiveStreamId,
31 
32     /// The stream is not currently expecting a frame of this type.
33     UnexpectedFrameType,
34 
35     /// The payload size is too big
36     PayloadTooBig,
37 
38     /// A header size is too big
39     HeaderTooBig,
40 
41     /// The application attempted to initiate too many streams to remote.
42     Rejected,
43 
44     /// The released capacity is larger than claimed capacity.
45     ReleaseCapacityTooBig,
46 
47     /// The stream ID space is overflowed.
48     ///
49     /// A new connection is needed.
50     OverflowedStreamId,
51 
52     /// Illegal headers, such as connection-specific headers.
53     MalformedHeaders,
54 
55     /// Request submitted with relative URI.
56     MissingUriSchemeAndAuthority,
57 
58     /// Calls `SendResponse::poll_reset` after having called `send_response`.
59     PollResetAfterSendResponse,
60 
61     /// Calls `PingPong::send_ping` before receiving a pong.
62     SendPingWhilePending,
63 
64     /// Tries to update local SETTINGS while ACK has not been received.
65     SendSettingsWhilePending,
66 }
67 
68 // ===== impl RecvError =====
69 
70 impl From<io::Error> for RecvError {
from(src: io::Error) -> Self71     fn from(src: io::Error) -> Self {
72         RecvError::Io(src)
73     }
74 }
75 
76 impl error::Error for RecvError {}
77 
78 impl fmt::Display for RecvError {
fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result79     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
80         use self::RecvError::*;
81 
82         match *self {
83             Connection(ref reason) => reason.fmt(fmt),
84             Stream { ref reason, .. } => reason.fmt(fmt),
85             Io(ref e) => e.fmt(fmt),
86         }
87     }
88 }
89 
90 // ===== impl SendError =====
91 
92 impl error::Error for SendError {}
93 
94 impl fmt::Display for SendError {
fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result95     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
96         use self::SendError::*;
97 
98         match *self {
99             User(ref e) => e.fmt(fmt),
100             Connection(ref reason) => reason.fmt(fmt),
101             Io(ref e) => e.fmt(fmt),
102         }
103     }
104 }
105 
106 impl From<io::Error> for SendError {
from(src: io::Error) -> Self107     fn from(src: io::Error) -> Self {
108         SendError::Io(src)
109     }
110 }
111 
112 impl From<UserError> for SendError {
from(src: UserError) -> Self113     fn from(src: UserError) -> Self {
114         SendError::User(src)
115     }
116 }
117 
118 // ===== impl UserError =====
119 
120 impl error::Error for UserError {}
121 
122 impl fmt::Display for UserError {
fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result123     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
124         use self::UserError::*;
125 
126         fmt.write_str(match *self {
127             InactiveStreamId => "inactive stream",
128             UnexpectedFrameType => "unexpected frame type",
129             PayloadTooBig => "payload too big",
130             HeaderTooBig => "header too big",
131             Rejected => "rejected",
132             ReleaseCapacityTooBig => "release capacity too big",
133             OverflowedStreamId => "stream ID overflowed",
134             MalformedHeaders => "malformed headers",
135             MissingUriSchemeAndAuthority => "request URI missing scheme and authority",
136             PollResetAfterSendResponse => "poll_reset after send_response is illegal",
137             SendPingWhilePending => "send_ping before received previous pong",
138             SendSettingsWhilePending => "sending SETTINGS before received previous ACK",
139         })
140     }
141 }
142