Skip to content

Commit

Permalink
Removed raise if failed check from the json and string response retur…
Browse files Browse the repository at this point in the history
…ns (#3986)

The previous logic raised an OSError if a response returned a status code of 400
or greater but it is useful to be able to retrieve the bodies from such
responses as they often contain additional information about the error
  • Loading branch information
owenlamont committed Jul 12, 2023
1 parent f996914 commit 822276d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
4 changes: 4 additions & 0 deletions docs/project/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ myst:
`pyodide-lock.json`
{pr}`3824`

- {{ Breaking }} Changed the FetchResponse body getter methods to no longer
throw an OSError exception for 400 and above response status codes
{pr}`3986`

### Packages

- OpenBLAS has been added and scipy now uses OpenBLAS rather than CLAPACK
Expand Down
4 changes: 0 additions & 4 deletions src/py/pyodide/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,6 @@ def url(self) -> str:
return self.js_response.url

def _raise_if_failed(self) -> None:
if self.js_response.status >= 400:
raise OSError(
f"Request for {self._url} failed with status {self.status}: {self.status_text}"
)
if self.js_response.bodyUsed:
raise OSError("Response body is already used")

Expand Down
20 changes: 20 additions & 0 deletions src/tests/test_pyodide_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@
from pytest_pyodide import run_in_pyodide


@pytest.fixture
def url_notfound(httpserver):
httpserver.expect_request("/data").respond_with_data(
b"404 Not Found",
content_type="text/text",
headers={"Access-Control-Allow-Origin": "*"},
status=404,
)
return httpserver.url_for("/data")


@pytest.mark.xfail_browsers(node="XMLHttpRequest is not available in node")
def test_open_url(selenium, httpserver):
httpserver.expect_request("/data").respond_with_data(
Expand Down Expand Up @@ -34,6 +45,15 @@ async def test_pyfetch_create_file(selenium):
)


@run_in_pyodide
async def test_pyfetch_return_400_status_body(selenium, url_notfound):
from pyodide.http import pyfetch

resp = await pyfetch(url_notfound)
body = await resp.string()
assert body == "404 Not Found"


@run_in_pyodide
async def test_pyfetch_unpack_archive(selenium):
import pathlib
Expand Down

0 comments on commit 822276d

Please sign in to comment.