Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update upload_artifact to emit HTTP error text #1815

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/hatch/index/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,19 @@ def upload_artifact(self, artifact: Path, data: dict):
files={'content': (artifact.name, f, 'application/octet-stream')},
auth=(self.user, self.auth),
)
response.raise_for_status()
if response.is_error:
import re

import httpx

# strip all html tags
post_res = re.sub(r'<[^>]+>', '', response.text)
exc_text = f'{response.status_code} {response.reason_phrase}\n{post_res}'
raise httpx.HTTPStatusError(
message=exc_text,
request=response.request,
response=response,
)

def get_simple_api(self, project: str) -> httpx.Response:
return self.client.get(
Expand Down
36 changes: 36 additions & 0 deletions tests/index/test_core.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from pathlib import Path
from unittest.mock import MagicMock

import httpx
import pytest

from hatch.index.core import PackageIndex
Expand Down Expand Up @@ -66,3 +70,35 @@ def test_client_cert_with_key(self, mocker):
_ = index.client

mock.assert_called_once_with(verify=True, cert=('foo', 'bar'), trust_env=True)


def test_upload_artifact_http_error():
package_index = PackageIndex(repo='')
mock_response = MagicMock()
mock_response.is_error = True
mock_response.status_code = 400
mock_response.reason_phrase = 'Bad Request'
# test real response content from pypi
mock_response.text = """
<html>
<head>
<title>400 This filename has already been used, use a different version. See https://test.pypi.org/help/#file-name-reuse for more information.</title>
</head>
<body>
<h1>400 This filename has already been used, use a different version. See https://test.pypi.org/help/#file-name-reuse for more information.</h1>
The server could not comply with the request since it is either malformed or otherwise incorrect.<br/><br/>
This filename has already been used, use a different version. See https://test.pypi.org/help/#file-name-reuse for more information.
</body>
</html>
"""
package_index.client.post = MagicMock(return_value=mock_response)
artifact = Path('dummy_artifact.txt')
artifact.write_text('dummy content')
data = {}
with pytest.raises(httpx.HTTPStatusError) as exc_info:
package_index.upload_artifact(artifact, data)
artifact.unlink()
assert (
'400 This filename has already been used, use a different version. See https://test.pypi.org/help/#file-name-reuse for more information.'
in str(exc_info.value)
)