Skip to content

Commit

Permalink
update upload_artifact to emit HTTP error text
Browse files Browse the repository at this point in the history
  • Loading branch information
ernimd committed Nov 18, 2024
1 parent 6bc4bbe commit c5e30ff
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
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)
)

0 comments on commit c5e30ff

Please sign in to comment.