From d88c5504f865284af4057de7698f251e2a379168 Mon Sep 17 00:00:00 2001 From: Joshua Salzedo Date: Wed, 20 Jul 2022 20:13:24 -0700 Subject: [PATCH 1/2] Improve information returned from upload errors --- src/poetry/publishing/uploader.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/poetry/publishing/uploader.py b/src/poetry/publishing/uploader.py index bed6b1f5928..c77a52b525c 100644 --- a/src/poetry/publishing/uploader.py +++ b/src/poetry/publishing/uploader.py @@ -38,7 +38,8 @@ class UploadError(Exception): def __init__(self, error: ConnectionError | HTTPError | str) -> None: if isinstance(error, HTTPError): 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): message = ( From 4327607ebaaa72637ee7e89110ad9a3dddcca89b Mon Sep 17 00:00:00 2001 From: Joshua Salzedo Date: Wed, 20 Jul 2022 20:38:08 -0700 Subject: [PATCH 2/2] Fix the tests to for new behavior, add one for this specific case --- tests/console/commands/test_publish.py | 2 +- tests/publishing/test_uploader.py | 23 +++++++++++++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/tests/console/commands/test_publish.py b/tests/console/commands/test_publish.py index 4ebd8286324..559f58f323f 100644 --- a/tests/console/commands/test_publish.py +++ b/tests/console/commands/test_publish.py @@ -36,7 +36,7 @@ def test_publish_returns_non_zero_code_for_upload_errors( Publishing simple-project (1.2.3) to PyPI """ 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() diff --git a/tests/publishing/test_uploader.py b/tests/publishing/test_uploader.py index 84581c4ffd4..9a7346825f3 100644 --- a/tests/publishing/test_uploader.py +++ b/tests/publishing/test_uploader.py @@ -32,7 +32,7 @@ def test_uploader_properly_handles_400_errors( with pytest.raises(UploadError) as e: 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( @@ -43,7 +43,26 @@ def test_uploader_properly_handles_403_errors( with pytest.raises(UploadError) as e: 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(