Skip to content

Commit

Permalink
Merge pull request scrapinghub#178 from scrapinghub/non-standard-stat…
Browse files Browse the repository at this point in the history
…us-codes

HttpClient: support non-standard response status codes
  • Loading branch information
kmike authored May 30, 2023
2 parents 773c3a7 + f36df71 commit c9f7525
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions web_poet/page_inputs/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ def _handle_status(
):
return

status = HTTPStatus(response.status)
msg = f"{response.status} {status.name} response for {response.url}"
status_name = _http_status_name(response.status)
msg = f"{response.status} {status_name} response for {response.url}"
raise HttpResponseError(msg, request=request, response=response)

async def request(
Expand Down Expand Up @@ -265,3 +265,18 @@ async def batch_execute(
def get_saved_responses(self) -> Iterable[_SavedResponseData]:
"""Return saved requests and responses."""
return self._saved_responses.values()


def _http_status_name(status: int):
"""
>>> _http_status_name(200)
'OK'
>>> _http_status_name(404)
'NOT_FOUND'
>>> _http_status_name(999)
'UNKNOWN'
"""
try:
return HTTPStatus(status).name
except ValueError:
return "UNKNOWN"

0 comments on commit c9f7525

Please sign in to comment.