Commit 95915f0a by Joshua Salzedo Committed by GitHub

Improve upload error output (#6043)

* Improve information returned from upload errors
* Fix the tests to for new behavior, add one for this specific case
parent 3e60aed7
...@@ -38,7 +38,8 @@ class UploadError(Exception): ...@@ -38,7 +38,8 @@ class UploadError(Exception):
def __init__(self, error: ConnectionError | HTTPError | str) -> None: def __init__(self, error: ConnectionError | HTTPError | str) -> None:
if isinstance(error, HTTPError): if isinstance(error, HTTPError):
message = ( message = (
f"HTTP Error {error.response.status_code}: {error.response.reason}" f"HTTP Error {error.response.status_code}: {error.response.reason} |"
f" {error.response.content!r}"
) )
elif isinstance(error, ConnectionError): elif isinstance(error, ConnectionError):
message = ( message = (
......
...@@ -36,7 +36,7 @@ def test_publish_returns_non_zero_code_for_upload_errors( ...@@ -36,7 +36,7 @@ def test_publish_returns_non_zero_code_for_upload_errors(
Publishing simple-project (1.2.3) to PyPI Publishing simple-project (1.2.3) to PyPI
""" """
expected_error_output = """\ expected_error_output = """\
HTTP Error 400: Bad Request HTTP Error 400: Bad Request | b'Bad Request'
""" """
assert expected_output in app_tester.io.fetch_output() assert expected_output in app_tester.io.fetch_output()
......
...@@ -32,7 +32,7 @@ def test_uploader_properly_handles_400_errors( ...@@ -32,7 +32,7 @@ def test_uploader_properly_handles_400_errors(
with pytest.raises(UploadError) as e: with pytest.raises(UploadError) as e:
uploader.upload("https://foo.com") uploader.upload("https://foo.com")
assert str(e.value) == "HTTP Error 400: Bad Request" assert str(e.value) == "HTTP Error 400: Bad Request | b'Bad request'"
def test_uploader_properly_handles_403_errors( def test_uploader_properly_handles_403_errors(
...@@ -43,7 +43,26 @@ def test_uploader_properly_handles_403_errors( ...@@ -43,7 +43,26 @@ def test_uploader_properly_handles_403_errors(
with pytest.raises(UploadError) as e: with pytest.raises(UploadError) as e:
uploader.upload("https://foo.com") uploader.upload("https://foo.com")
assert str(e.value) == "HTTP Error 403: Forbidden" assert str(e.value) == "HTTP Error 403: Forbidden | b'Unauthorized'"
def test_uploader_properly_handles_nonstandard_errors(
http: type[httpretty.httpretty], uploader: Uploader
):
# content based off a true story.
# Message changed to protect the ~~innocent~~ guilty.
content = (
b'{\n "errors": [ {\n '
b'"status": 400,'
b'"message": "I cant let you do that, dave"\n'
b"} ]\n}"
)
http.register_uri(http.POST, "https://foo.com", status=400, body=content)
with pytest.raises(UploadError) as e:
uploader.upload("https://foo.com")
assert str(e.value) == f"HTTP Error 400: Bad Request | {content}"
def test_uploader_properly_handles_301_redirects( def test_uploader_properly_handles_301_redirects(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment