1+ //! Crate's error module.
2+ //!
3+ //! Contains all error types used throughout the crate.
4+
15use core:: convert:: Infallible ;
26
7+ /// Error decoding a WebSocket frame.
38#[ derive( Debug , thiserror:: Error ) ]
49pub enum FrameDecodeError {
10+ /// Reserved bits are not zero.
511 #[ error( "Reserved bits must be zero" ) ]
612 ReservedBitsNotZero ,
13+ /// Unmasked frame received from client.
14+ ///
715 /// The server must close the connection when an unmasked frame is received.
816 #[ error( "Received an unmasked frame from client" ) ]
917 UnmaskedFrameFromClient ,
18+ /// Masked frame received from server.
19+ ///
1020 /// The client must close the connection when a masked frame is received.
1121 #[ error( "Received a masked frame from server" ) ]
1222 MaskedFrameFromServer ,
23+ /// Invalid opcode.
1324 #[ error( "Invalid opcode" ) ]
1425 InvalidOpCode ,
15- // The payload length comes as a u64, converting it to usize might fail on 32-bit systems
26+ /// Payload length is too large.
27+ // XXX: The payload length comes as a u64, converting it to usize might fail on 32-bit systems
1628 #[ error( "Payload too large" ) ]
1729 PayloadTooLarge ,
30+ /// Control frame fragmented.
31+ ///
32+ /// Control frames must not be fragmented.
1833 #[ error( "Control frame fragmented" ) ]
1934 ControlFrameFragmented ,
35+ /// Control frame too large.
36+ ///
37+ /// Control frames must have a payload length of 125 bytes or less.
2038 #[ error( "Control frame too large" ) ]
2139 ControlFrameTooLarge ,
2240}
2341
42+ /// Error encoding a WebSocket frame.
2443#[ derive( Debug , thiserror:: Error ) ]
2544pub enum FrameEncodeError {
45+ /// Write buffer is too small to hold the encoded frame.
2646 #[ error( "Buffer too small" ) ]
2747 BufferTooSmall ,
2848}
2949
50+ /// Error decoding an HTTP request/response.
3051#[ derive( Debug , thiserror:: Error ) ]
3152pub enum HttpDecodeError {
53+ /// Error parsing the HTTP request/response.
3254 #[ error( "Parse error: {0}" ) ]
3355 Parse ( httparse:: Error ) ,
3456}
@@ -39,63 +61,83 @@ impl From<httparse::Error> for HttpDecodeError {
3961 }
4062}
4163
64+ /// Error encoding an HTTP request/response.
4265#[ derive( Debug , thiserror:: Error ) ]
4366pub enum HttpEncodeError {
67+ /// Write buffer is too small to hold the encoded HTTP request/response.
4468 #[ error( "Buffer too small" ) ]
4569 BufferTooSmall ,
4670}
4771
72+ /// Protocol specific errors/violations.
4873#[ derive( Debug , thiserror:: Error ) ]
4974pub enum ProtocolError {
75+ /// Close frame is invalid.
5076 #[ error( "Invalid close frame" ) ]
5177 InvalidCloseFrame ,
78+ /// Close code is invalid.
5279 #[ error( "Invalid close code" ) ]
5380 InvalidCloseCode ,
81+ /// Text message contains invalid UTF-8.
5482 #[ error( "Invalid UTF-8" ) ]
5583 InvalidUTF8 ,
84+ /// Fragment is invalid.
85+ ///
86+ /// This happens when a final fragment is received without any prior fragments.
5687 #[ error( "Invalid fragment" ) ]
5788 InvalidFragment ,
89+ /// Continuation frame is invalid.
90+ ///
91+ /// This happens when a continuation frame is received without an ongoing fragmented message.
5892 #[ error( "Invalid continuation frame" ) ]
5993 InvalidContinuationFrame ,
6094}
6195
96+ /// Error reading from a WebSocket connection.
6297#[ derive( Debug , thiserror:: Error ) ]
6398pub enum ReadError < I > {
99+ /// Error reading a WebSocket frame from the underlying I/O.
64100 #[ error( "Read frame error: {0}" ) ]
65101 ReadFrame (
66102 #[ source]
67103 #[ from]
68104 framez:: ReadError < I , FrameDecodeError > ,
69105 ) ,
106+ /// Error reading an HTTP request/response from the underlying I/O.
70107 #[ error( "Read http error: {0}" ) ]
71108 ReadHttp (
72109 #[ source]
73110 #[ from]
74111 framez:: ReadError < I , HttpDecodeError > ,
75112 ) ,
113+ /// Protocol error.
76114 #[ error( "Protocol error: {0}" ) ]
77115 Protocol (
78116 #[ source]
79117 #[ from]
80118 ProtocolError ,
81119 ) ,
120+ /// Fragments buffer is too small to read a frame.
82121 #[ error( "Fragments buffer too small to read a frame" ) ]
83122 FragmentsBufferTooSmall ,
84123}
85124
125+ /// Error writing to a WebSocket connection.
86126#[ derive( Debug , thiserror:: Error ) ]
87127pub enum WriteError < I > {
88128 /// Websocket connection is closed.
89129 ///
90- /// To close the TCP connection, you should drop the [`WebSocket`](crate::WebSocket) instance.
130+ /// To close the TCP connection, you should drop/close the underlying I/O instance.
91131 #[ error( "Connection closed" ) ]
92132 ConnectionClosed ,
133+ /// Error writing a WebSocket frame to the underlying I/O.
93134 #[ error( "Write frame error: {0}" ) ]
94135 WriteFrame (
95136 #[ source]
96137 #[ from]
97138 framez:: WriteError < I , FrameEncodeError > ,
98139 ) ,
140+ /// Error writing an HTTP request/response to the underlying I/O.
99141 #[ error( "Write http error: {0}" ) ]
100142 WriteHttp (
101143 #[ source]
@@ -104,6 +146,11 @@ pub enum WriteError<I> {
104146 ) ,
105147}
106148
149+ /// Error establishing a WebSocket handshake.
150+ ///
151+ /// # Generic Parameter
152+ ///
153+ /// `E`: User-defined error type for custom errors during the handshake.
107154#[ derive( Debug , thiserror:: Error ) ]
108155pub enum HandshakeError < E = Infallible > {
109156 /// Use of the wrong HTTP method (the WebSocket protocol requires the GET method to be used).
@@ -112,52 +159,72 @@ pub enum HandshakeError<E = Infallible> {
112159 /// Wrong HTTP version used (the WebSocket protocol requires version 1.1 or higher).
113160 #[ error( "HTTP version must be 1.1 or higher" ) ]
114161 WrongHttpVersion ,
162+ /// Connection was closed during the handshake.
115163 #[ error( "Connection closed during handshake" ) ]
116164 ConnectionClosed ,
165+ /// Invalid status code. (Should be 101 for switching protocols.)
117166 #[ error( "Invalid status code" ) ]
118167 InvalidStatusCode ,
168+ /// Missing or invalid (`Upgrade`: `websocket`) header.
119169 #[ error( "Missing or invalid upgrade header" ) ]
120170 MissingOrInvalidUpgrade ,
171+ /// Missing or invalid (`Connection`: `upgrade`) header.
121172 #[ error( "Missing or invalid connection header" ) ]
122173 MissingOrInvalidConnection ,
174+ /// Missing or invalid (`Sec-WebSocket-Accept`) header.
123175 #[ error( "Missing or invalid sec websocket accept header" ) ]
124176 MissingOrInvalidAccept ,
177+ /// Missing or invalid (`Sec-WebSocket-Version`) header.
125178 #[ error( "Missing or invalid sec websocket version header" ) ]
126179 MissingOrInvalidSecVersion ,
180+ /// Missing (`Sec-WebSocket-Key`) header.
127181 #[ error( "Missing sec websocket key header" ) ]
128182 MissingSecKey ,
183+ /// Other error.
184+ ///
185+ /// User-defined error type.
129186 #[ error( "Other: {0}" ) ]
130187 Other ( E ) ,
131188}
132189
190+ /// Fragmentation error.
133191#[ derive( Debug , thiserror:: Error ) ]
134192pub enum FragmentationError {
193+ /// Fragment size is zero.
135194 #[ error( "Fragment size must be greater than 0" ) ]
136195 InvalidFragmentSize ,
196+ /// Error indicating that a message type that cannot be fragmented was attempted to be fragmented.
197+ ///
198+ /// Only text and binary messages can be fragmented.
137199 #[ error( "Only text and binary messages can be fragmented" ) ]
138200 CanNotBeFragmented ,
139201}
140202
203+ /// General WebSocket error type.
141204#[ derive( Debug , thiserror:: Error ) ]
142205pub enum Error < I , E = Infallible > {
206+ /// Error reading from the WebSocket connection.
143207 #[ error( "Read error: {0}" ) ]
144208 Read (
145209 #[ from]
146210 #[ source]
147211 ReadError < I > ,
148212 ) ,
213+ /// Error writing to the WebSocket connection.
149214 #[ error( "Write error: {0}" ) ]
150215 Write (
151216 #[ from]
152217 #[ source]
153218 WriteError < I > ,
154219 ) ,
220+ /// Handshake error.
155221 #[ error( "Handshake error: {0}" ) ]
156222 Handshake (
157223 #[ from]
158224 #[ source]
159225 HandshakeError < E > ,
160226 ) ,
227+ /// Fragmentation error.
161228 #[ error( "Fragment error: {0}" ) ]
162229 Fragmentation (
163230 #[ from]
0 commit comments