Esempio n. 1
0
def test_request_http_error(
    mock_raise_for_status: mock.Mock, caplog: pytest.LogCaptureFixture
) -> None:
    caplog.set_level(logging.DEBUG)
    link = Link("http://localhost")
    session = mock.Mock(PipSession)
    session.get.return_value = mock.Mock()
    mock_raise_for_status.side_effect = NetworkConnectionError("Http error")
    assert _get_html_page(link, session=session) is None
    assert "Could not fetch URL http://localhost: Http error - skipping" in caplog.text
Esempio n. 2
0
def raise_for_status(resp):

    # type: (Response) -> None

    http_error_msg = ''

    if isinstance(resp.reason, bytes):

        # We attempt to decode utf-8 first because some servers

        # choose to localize their reason strings. If the string

        # isn't utf-8, we fall back to iso-8859-1 for all other

        # encodings.

        try:

            reason = resp.reason.decode('utf-8')

        except UnicodeDecodeError:

            reason = resp.reason.decode('iso-8859-1')

    else:

        reason = resp.reason

    if 400 <= resp.status_code < 500:

        http_error_msg = '%s Client Error: %s for url: %s' % (resp.status_code,
                                                              reason, resp.url)

    elif 500 <= resp.status_code < 600:

        http_error_msg = '%s Server Error: %s for url: %s' % (resp.status_code,
                                                              reason, resp.url)

    if http_error_msg:

        raise NetworkConnectionError(http_error_msg, response=resp)
def raise_for_status(resp: Response) -> None:
    http_error_msg = ""
    if isinstance(resp.reason, bytes):
        # We attempt to decode utf-8 first because some servers
        # choose to localize their reason strings. If the string
        # isn't utf-8, we fall back to iso-8859-1 for all other
        # encodings.
        try:
            reason = resp.reason.decode("utf-8")
        except UnicodeDecodeError:
            reason = resp.reason.decode("iso-8859-1")
    else:
        reason = resp.reason

    if 400 <= resp.status_code < 500:
        http_error_msg = (
            f"{resp.status_code} Client Error: {reason} for url: {resp.url}")

    elif 500 <= resp.status_code < 600:
        http_error_msg = (
            f"{resp.status_code} Server Error: {reason} for url: {resp.url}")

    if http_error_msg:
        raise NetworkConnectionError(http_error_msg, response=resp)