1"""
2_exceptions.py
3websocket - WebSocket client library for Python
4
5Copyright 2025 engn33r
6
7Licensed under the Apache License, Version 2.0 (the "License");
8you may not use this file except in compliance with the License.
9You may obtain a copy of the License at
10
11 http://www.apache.org/licenses/LICENSE-2.0
12
13Unless required by applicable law or agreed to in writing, software
14distributed under the License is distributed on an "AS IS" BASIS,
15WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16See the License for the specific language governing permissions and
17limitations under the License.
18"""
19
20from typing import Any, Optional
21
22
23class WebSocketException(Exception):
24 """
25 WebSocket exception class.
26 """
27
28 pass
29
30
31class WebSocketProtocolException(WebSocketException):
32 """
33 If the WebSocket protocol is invalid, this exception will be raised.
34 """
35
36 pass
37
38
39class WebSocketPayloadException(WebSocketException):
40 """
41 If the WebSocket payload is invalid, this exception will be raised.
42 """
43
44 pass
45
46
47class WebSocketConnectionClosedException(WebSocketException):
48 """
49 If remote host closed the connection or some network error happened,
50 this exception will be raised.
51 """
52
53 pass
54
55
56class WebSocketTimeoutException(WebSocketException):
57 """
58 WebSocketTimeoutException will be raised at socket timeout during read/write data.
59 """
60
61 pass
62
63
64class WebSocketProxyException(WebSocketException):
65 """
66 WebSocketProxyException will be raised when proxy error occurred.
67 """
68
69 pass
70
71
72class WebSocketBadStatusException(WebSocketException):
73 """
74 WebSocketBadStatusException will be raised when we get bad handshake status code.
75 """
76
77 def __init__(
78 self,
79 message: str,
80 status_code: int,
81 status_message: Optional[str] = None,
82 resp_headers: Optional[dict] = None,
83 resp_body: Optional[bytes] = None,
84 ) -> None:
85 super().__init__(message)
86 self.status_code = status_code
87 self.resp_headers = resp_headers
88 self.resp_body = resp_body
89
90
91class WebSocketAddressException(WebSocketException):
92 """
93 If the websocket address info cannot be found, this exception will be raised.
94 """
95
96 pass