1
1
"""
2
- :mod:`websockets.exceptions` defines the following exception hierarchy:
2
+ :mod:`websockets.exceptions` defines the following hierarchy of exceptions.
3
3
4
4
* :exc:`WebSocketException`
5
5
* :exc:`ConnectionClosed`
6
- * :exc:`ConnectionClosedError`
7
6
* :exc:`ConnectionClosedOK`
7
+ * :exc:`ConnectionClosedError`
8
+ * :exc:`InvalidURI`
8
9
* :exc:`InvalidHandshake`
9
10
* :exc:`SecurityError`
10
11
* :exc:`InvalidMessage` (legacy)
12
+ * :exc:`InvalidStatus`
13
+ * :exc:`InvalidStatusCode` (legacy)
11
14
* :exc:`InvalidHeader`
12
15
* :exc:`InvalidHeaderFormat`
13
16
* :exc:`InvalidHeaderValue`
14
17
* :exc:`InvalidOrigin`
15
18
* :exc:`InvalidUpgrade`
16
- * :exc:`InvalidStatus`
17
- * :exc:`InvalidStatusCode` (legacy)
18
19
* :exc:`NegotiationError`
19
20
* :exc:`DuplicateParameter`
20
21
* :exc:`InvalidParameterName`
21
22
* :exc:`InvalidParameterValue`
22
23
* :exc:`AbortHandshake` (legacy)
23
24
* :exc:`RedirectHandshake` (legacy)
24
- * :exc:`InvalidState`
25
- * :exc:`InvalidURI`
26
- * :exc:`PayloadTooBig`
27
- * :exc:`ProtocolError`
25
+ * :exc:`ProtocolError` (Sans-I/O)
26
+ * :exc:`PayloadTooBig` (Sans-I/O)
27
+ * :exc:`InvalidState` (Sans-I/O)
28
28
29
29
"""
30
30
40
40
__all__ = [
41
41
"WebSocketException" ,
42
42
"ConnectionClosed" ,
43
- "ConnectionClosedError" ,
44
43
"ConnectionClosedOK" ,
44
+ "ConnectionClosedError" ,
45
+ "InvalidURI" ,
45
46
"InvalidHandshake" ,
46
47
"SecurityError" ,
47
48
"InvalidMessage" ,
49
+ "InvalidStatus" ,
50
+ "InvalidStatusCode" ,
48
51
"InvalidHeader" ,
49
52
"InvalidHeaderFormat" ,
50
53
"InvalidHeaderValue" ,
51
54
"InvalidOrigin" ,
52
55
"InvalidUpgrade" ,
53
- "InvalidStatus" ,
54
- "InvalidStatusCode" ,
55
56
"NegotiationError" ,
56
57
"DuplicateParameter" ,
57
58
"InvalidParameterName" ,
58
59
"InvalidParameterValue" ,
59
60
"AbortHandshake" ,
60
61
"RedirectHandshake" ,
61
- "InvalidState" ,
62
- "InvalidURI" ,
63
- "PayloadTooBig" ,
64
62
"ProtocolError" ,
65
63
"WebSocketProtocolError" ,
64
+ "PayloadTooBig" ,
65
+ "InvalidState" ,
66
66
]
67
67
68
68
@@ -139,6 +139,16 @@ def reason(self) -> str:
139
139
return self .rcvd .reason
140
140
141
141
142
+ class ConnectionClosedOK (ConnectionClosed ):
143
+ """
144
+ Like :exc:`ConnectionClosed`, when the connection terminated properly.
145
+
146
+ A close code with code 1000 (OK) or 1001 (going away) or without a code was
147
+ received and sent.
148
+
149
+ """
150
+
151
+
142
152
class ConnectionClosedError (ConnectionClosed ):
143
153
"""
144
154
Like :exc:`ConnectionClosed`, when the connection terminated with an error.
@@ -149,19 +159,23 @@ class ConnectionClosedError(ConnectionClosed):
149
159
"""
150
160
151
161
152
- class ConnectionClosedOK ( ConnectionClosed ):
162
+ class InvalidURI ( WebSocketException ):
153
163
"""
154
- Like :exc:`ConnectionClosed`, when the connection terminated properly.
155
-
156
- A close code with code 1000 (OK) or 1001 (going away) or without a code was
157
- received and sent.
164
+ Raised when connecting to a URI that isn't a valid WebSocket URI.
158
165
159
166
"""
160
167
168
+ def __init__ (self , uri : str , msg : str ) -> None :
169
+ self .uri = uri
170
+ self .msg = msg
171
+
172
+ def __str__ (self ) -> str :
173
+ return f"{ self .uri } isn't a valid URI: { self .msg } "
174
+
161
175
162
176
class InvalidHandshake (WebSocketException ):
163
177
"""
164
- Raised during the handshake when the WebSocket connection fails.
178
+ Base class for exceptions raised when the opening handshake fails.
165
179
166
180
"""
167
181
@@ -170,10 +184,27 @@ class SecurityError(InvalidHandshake):
170
184
"""
171
185
Raised when a handshake request or response breaks a security rule.
172
186
173
- Security limits are hard coded.
187
+ Security limits can be configured with :doc:`environment variables
188
+ <../reference/variables>`.
189
+
190
+ """
191
+
192
+
193
+ class InvalidStatus (InvalidHandshake ):
194
+ """
195
+ Raised when a handshake response rejects the WebSocket upgrade.
174
196
175
197
"""
176
198
199
+ def __init__ (self , response : http11 .Response ) -> None :
200
+ self .response = response
201
+
202
+ def __str__ (self ) -> str :
203
+ return (
204
+ "server rejected WebSocket connection: "
205
+ f"HTTP { self .response .status_code :d} "
206
+ )
207
+
177
208
178
209
class InvalidHeader (InvalidHandshake ):
179
210
"""
@@ -210,7 +241,7 @@ class InvalidHeaderValue(InvalidHeader):
210
241
"""
211
242
Raised when an HTTP header has a wrong value.
212
243
213
- The format of the header is correct but a value isn't acceptable.
244
+ The format of the header is correct but the value isn't acceptable.
214
245
215
246
"""
216
247
@@ -232,25 +263,9 @@ class InvalidUpgrade(InvalidHeader):
232
263
"""
233
264
234
265
235
- class InvalidStatus (InvalidHandshake ):
236
- """
237
- Raised when a handshake response rejects the WebSocket upgrade.
238
-
239
- """
240
-
241
- def __init__ (self , response : http11 .Response ) -> None :
242
- self .response = response
243
-
244
- def __str__ (self ) -> str :
245
- return (
246
- "server rejected WebSocket connection: "
247
- f"HTTP { self .response .status_code :d} "
248
- )
249
-
250
-
251
266
class NegotiationError (InvalidHandshake ):
252
267
"""
253
- Raised when negotiating an extension fails.
268
+ Raised when negotiating an extension or a subprotocol fails.
254
269
255
270
"""
256
271
@@ -300,41 +315,42 @@ def __str__(self) -> str:
300
315
return f"invalid value for parameter { self .name } : { self .value } "
301
316
302
317
303
- class InvalidState (WebSocketException , AssertionError ):
318
+ class ProtocolError (WebSocketException ):
304
319
"""
305
- Raised when an operation is forbidden in the current state .
320
+ Raised when receiving or sending a frame that breaks the protocol .
306
321
307
- This exception is an implementation detail.
322
+ The Sans-I/O implementation raises this exception when:
308
323
309
- It should never be raised in normal circumstances.
324
+ * receiving or sending a frame that contains invalid data;
325
+ * receiving or sending an invalid sequence of frames.
310
326
311
327
"""
312
328
313
329
314
- class InvalidURI (WebSocketException ):
330
+ class PayloadTooBig (WebSocketException ):
315
331
"""
316
- Raised when connecting to a URI that isn't a valid WebSocket URI .
332
+ Raised when parsing a frame with a payload that exceeds the maximum size .
317
333
318
- """
319
-
320
- def __init__ (self , uri : str , msg : str ) -> None :
321
- self .uri = uri
322
- self .msg = msg
323
-
324
- def __str__ (self ) -> str :
325
- return f"{ self .uri } isn't a valid URI: { self .msg } "
334
+ The Sans-I/O layer uses this exception internally. It doesn't bubble up to
335
+ the I/O layer.
326
336
337
+ The :meth:`~websockets.extensions.Extension.decode` method of extensions
338
+ must raise :exc:`PayloadTooBig` if decoding a frame would exceed the limit.
327
339
328
- class PayloadTooBig (WebSocketException ):
329
340
"""
330
- Raised when receiving a frame with a payload exceeding the maximum size.
331
341
342
+
343
+ class InvalidState (WebSocketException , AssertionError ):
332
344
"""
345
+ Raised when sending a frame is forbidden in the current state.
333
346
347
+ Specifically, the Sans-I/O layer raises this exception when:
334
348
335
- class ProtocolError (WebSocketException ):
336
- """
337
- Raised when a frame breaks the protocol.
349
+ * sending a data frame to a connection in a state other
350
+ :attr:`~websockets.protocol.State.OPEN`;
351
+ * sending a control frame to a connection in a state other than
352
+ :attr:`~websockets.protocol.State.OPEN` or
353
+ :attr:`~websockets.protocol.State.CLOSING`.
338
354
339
355
"""
340
356
0 commit comments