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     /// Tries to send push promise to peer who has disabled server push
68     PeerDisabledServerPush,
69 }
70 
71 // ===== impl RecvError =====
72 
73 impl From<io::Error> for RecvError {
from(src: io::Error) -> Self74     fn from(src: io::Error) -> Self {
75         RecvError::Io(src)
76     }
77 }
78 
79 impl error::Error for RecvError {}
80 
81 impl fmt::Display for RecvError {
fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result82     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
83         use self::RecvError::*;
84 
85         match *self {
86             Connection(ref reason) => reason.fmt(fmt),
87             Stream { ref reason, .. } => reason.fmt(fmt),
88             Io(ref e) => e.fmt(fmt),
89         }
90     }
91 }
92 
93 // ===== impl SendError =====
94 
95 impl error::Error for SendError {}
96 
97 impl fmt::Display for SendError {
fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result98     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
99         use self::SendError::*;
100 
101         match *self {
102             User(ref e) => e.fmt(fmt),
103             Connection(ref reason) => reason.fmt(fmt),
104             Io(ref e) => e.fmt(fmt),
105         }
106     }
107 }
108 
109 impl From<io::Error> for SendError {
from(src: io::Error) -> Self110     fn from(src: io::Error) -> Self {
111         SendError::Io(src)
112     }
113 }
114 
115 impl From<UserError> for SendError {
from(src: UserError) -> Self116     fn from(src: UserError) -> Self {
117         SendError::User(src)
118     }
119 }
120 
121 // ===== impl UserError =====
122 
123 impl error::Error for UserError {}
124 
125 impl fmt::Display for UserError {
fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result126     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
127         use self::UserError::*;
128 
129         fmt.write_str(match *self {
130             InactiveStreamId => "inactive stream",
131             UnexpectedFrameType => "unexpected frame type",
132             PayloadTooBig => "payload too big",
133             HeaderTooBig => "header too big",
134             Rejected => "rejected",
135             ReleaseCapacityTooBig => "release capacity too big",
136             OverflowedStreamId => "stream ID overflowed",
137             MalformedHeaders => "malformed headers",
138             MissingUriSchemeAndAuthority => "request URI missing scheme and authority",
139             PollResetAfterSendResponse => "poll_reset after send_response is illegal",
140             SendPingWhilePending => "send_ping before received previous pong",
141             SendSettingsWhilePending => "sending SETTINGS before received previous ACK",
142             PeerDisabledServerPush => "sending PUSH_PROMISE to peer who disabled server push",
143         })
144     }
145 }
146