Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit fe96d96

Browse files
committedAug 6, 2024·
Don't allow creation of HttpStatusError with invalid values.
Because I was creating dummy errors somewhere else with an error code of 123 and took me 30min to figure out why the repr of the errors were empty.
1 parent 38d7b62 commit fe96d96

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed
 

‎src/py/pyodide/http.py

+3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ def __init__(self, status: int, status_text: str, url: str) -> None:
4040
super().__init__(f"{status} Client Error: {status_text} for url: {url}")
4141
elif 500 <= status < 600:
4242
super().__init__(f"{status} Server Error: {status_text} for url: {url}")
43+
raise ValueError(
44+
f"Invalid error code not comprised between 400 and 599: {status}"
45+
)
4346

4447

4548
class BodyUsedError(OSError):

‎src/tests/test_pyodide_http.py

+12
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,18 @@ async def test_pyfetch_raise_for_status_does_not_raise_200(
113113
assert error_504.value.url.endswith("status_504")
114114

115115

116+
@run_in_pyodide
117+
async def cant_create_invalid_HttpStatusErrors(selenium):
118+
from pyodide.http import HttpStatusError,
119+
120+
with pytest.raises(ValueError, match="Invalid error code not comprised between 400 and 599"):
121+
HttpStatusError(200, "Can't raise a success error code")
122+
123+
with pytest.raises(ValueError, match="Invalid error code not comprised between 400 and 599"):
124+
HttpStatusError(999, "Can't raise an unknown error code")
125+
126+
127+
116128
@run_in_pyodide
117129
async def test_pyfetch_unpack_archive(selenium):
118130
import pathlib

0 commit comments

Comments
 (0)
Please sign in to comment.