Skip to content

Commit 6a4059c

Browse files
committed
HttpStatusError: store status code
It can be convenient to access after the fact instead of passing the msg. Closes pyodide#4973
1 parent 01ca727 commit 6a4059c

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

docs/project/changelog.md

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ myst:
2121
timezone handling.
2222
{pr}`4889`
2323

24+
- {{ Enhancement }} `HttpStatusError` now store their the corresponding request
25+
`status`, `status_message` and `url` {pr}`4974`.
26+
2427
- {{ Enhancement }} Added implementation to abort `pyfetch` and `FetchResponse`
2528
manually or automatically.
2629
{pr}`4846`

src/py/pyodide/http.py

+7
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,14 @@
2828

2929

3030
class HttpStatusError(OSError):
31+
status: int
32+
status_text: str
33+
url: str
34+
3135
def __init__(self, status: int, status_text: str, url: str) -> None:
36+
self.status = status
37+
self.status_text = status_text
38+
self.url = urls
3239
if 400 <= status < 500:
3340
super().__init__(f"{status} Client Error: {status_text} for url: {url}")
3441
elif 500 <= status < 600:

src/tests/test_pyodide_http.py

+16-5
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ def raise_for_status_fixture(httpserver):
8080

8181

8282
@run_in_pyodide
83-
async def test_pyfetch_raise_for_status(selenium, raise_for_status_fixture):
83+
async def test_pyfetch_raise_for_status_does_not_raise_200(
84+
selenium, raise_for_status_fixture
85+
):
8486
import pytest
8587

8688
from pyodide.http import pyfetch
@@ -91,16 +93,25 @@ async def test_pyfetch_raise_for_status(selenium, raise_for_status_fixture):
9193

9294
resp = await pyfetch(raise_for_status_fixture["/status_404"])
9395
with pytest.raises(
94-
OSError, match="404 Client Error: NOT FOUND for url: .*/status_404"
95-
):
96+
HttpStatusError, match="404 Client Error: NOT FOUND for url: .*/status_404"
97+
) as error_404:
9698
resp.raise_for_status()
9799

100+
assert error_404.status == 404
101+
assert error_404.status_text == "NOT FOUND"
102+
assert error_404.url.endswith("status_404")
103+
98104
resp = await pyfetch(raise_for_status_fixture["/status_504"])
99105
with pytest.raises(
100-
OSError, match="504 Server Error: GATEWAY TIMEOUT for url: .*/status_504"
101-
):
106+
HttpStatusError,
107+
match="504 Server Error: GATEWAY TIMEOUT for url: .*/status_504",
108+
) as error_504:
102109
resp.raise_for_status()
103110

111+
assert error_504.status == 504
112+
assert error_504.status_text == "GATEWAY TIMEOUT"
113+
assert error_504.url.endswith("status_504")
114+
104115

105116
@run_in_pyodide
106117
async def test_pyfetch_unpack_archive(selenium):

0 commit comments

Comments
 (0)