Skip to content

Commit d63ed11

Browse files
committed
Make error messages more error friendly.
Avoid weird exception chaining to assertion errors.
1 parent 1b0b2de commit d63ed11

File tree

4 files changed

+25
-12
lines changed

4 files changed

+25
-12
lines changed

docs/project/changelog.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,15 @@ They may change at any time.
5656
If you raise :exc:`~exceptions.ConnectionClosed` or a subclass — rather
5757
than catch them when websockets raises them — you must change your code.
5858

59+
.. note::
60+
61+
**Version 10.0 adds a ``msg`` parameter to** ``InvalidURI.__init__`` **.**
62+
63+
If you raise :exc:`~exceptions.InvalidURI` — rather than catch them when
64+
websockets raises them — you must change your code.
65+
66+
Also:
67+
5968
* Added compatibility with Python 3.10.
6069

6170
* Added :func:`~websockets.broadcast` to send a message to many clients.
@@ -150,6 +159,8 @@ They may change at any time.
150159
from websockets.client import connect
151160
from websockets.server import serve
152161

162+
Also:
163+
153164
* Added compatibility with Python 3.9.
154165

155166
* Added support for IRIs in addition to URIs.

src/websockets/exceptions.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,11 +374,12 @@ class InvalidURI(WebSocketException):
374374
375375
"""
376376

377-
def __init__(self, uri: str) -> None:
377+
def __init__(self, uri: str, msg: str) -> None:
378378
self.uri = uri
379+
self.msg = msg
379380

380381
def __str__(self) -> str:
381-
return f"{self.uri} isn't a valid URI"
382+
return f"{self.uri} isn't a valid URI: {self.msg}"
382383

383384

384385
class PayloadTooBig(WebSocketException):

src/websockets/uri.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,26 +54,27 @@ def parse_uri(uri: str) -> WebSocketURI:
5454
5555
"""
5656
parsed = urllib.parse.urlparse(uri)
57-
try:
58-
assert parsed.scheme in ["ws", "wss"]
59-
assert parsed.params == ""
60-
assert parsed.fragment == ""
61-
assert parsed.hostname is not None
62-
except AssertionError as exc:
63-
raise exceptions.InvalidURI(uri) from exc
57+
if parsed.scheme not in ["ws", "wss"]:
58+
raise exceptions.InvalidURI(uri, "scheme isn't ws or wss")
59+
if parsed.hostname is None:
60+
raise exceptions.InvalidURI(uri, "hostame isn't provided")
61+
if parsed.fragment != "":
62+
raise exceptions.InvalidURI(uri, "fragment identifier is meaningless")
6463

6564
secure = parsed.scheme == "wss"
6665
host = parsed.hostname
6766
port = parsed.port or (443 if secure else 80)
6867
resource_name = parsed.path or "/"
68+
if parsed.params:
69+
resource_name += ";" + parsed.params
6970
if parsed.query:
7071
resource_name += "?" + parsed.query
7172
user_info = None
7273
if parsed.username is not None:
7374
# urllib.parse.urlparse accepts URLs with a username but without a
7475
# password. This doesn't make sense for HTTP Basic Auth credentials.
7576
if parsed.password is None:
76-
raise exceptions.InvalidURI(uri)
77+
raise exceptions.InvalidURI(uri, "username provided without password")
7778
user_info = (parsed.username, parsed.password)
7879

7980
try:

tests/test_exceptions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,8 @@ def test_str(self):
142142
"WebSocket connection isn't established yet",
143143
),
144144
(
145-
InvalidURI("|"),
146-
"| isn't a valid URI",
145+
InvalidURI("|", "not at all!"),
146+
"| isn't a valid URI: not at all!",
147147
),
148148
(
149149
PayloadTooBig("payload length exceeds limit: 2 > 1 bytes"),

0 commit comments

Comments
 (0)