Skip to content

Commit

Permalink
better errors on upload failure
Browse files Browse the repository at this point in the history
preserve the underlying exception from requests, so that it is available
when running with `--verbose`

fixes #3813, supersedes #7004
  • Loading branch information
dimbleby authored and radoering committed Sep 29, 2024
1 parent cfe41d6 commit 64109a7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 25 deletions.
34 changes: 13 additions & 21 deletions src/poetry/publishing/uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

from poetry.core.masonry.metadata import Metadata
from poetry.core.masonry.utils.helpers import distribution_name
from requests.exceptions import ConnectionError
from requests.exceptions import HTTPError
from requests_toolbelt import user_agent
from requests_toolbelt.multipart import MultipartEncoder
from requests_toolbelt.multipart import MultipartEncoderMonitor
Expand All @@ -27,23 +25,7 @@


class UploadError(Exception):
def __init__(self, error: ConnectionError | HTTPError | str) -> None:
if isinstance(error, HTTPError):
if error.response is None:
message = "HTTP Error connecting to the repository"
else:
message = (
f"HTTP Error {error.response.status_code}: "
f"{error.response.reason} | {error.response.content!r}"
)
elif isinstance(error, ConnectionError):
message = (
"Connection Error: We were unable to connect to the repository, "
"ensure the url is correct and can be reached."
)
else:
message = error
super().__init__(message)
pass


class Uploader:
Expand Down Expand Up @@ -268,12 +250,22 @@ def _upload_file(
bar.display()
else:
resp.raise_for_status()
except (requests.ConnectionError, requests.HTTPError) as e:

except requests.RequestException as e:
if self._io.output.is_decorated():
self._io.overwrite(
f" - Uploading <c1>{file.name}</c1> <error>FAILED</>"
)
raise UploadError(e)

if e.response is not None:
message = (
f"HTTP Error {e.response.status_code}: "
f"{e.response.reason} | {e.response.content!r}"
)
raise UploadError(message) from e

raise UploadError("Error connecting to repository") from e

finally:
self._io.write_line("")

Expand Down
5 changes: 1 addition & 4 deletions tests/console/commands/test_publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import requests

from poetry.factory import Factory
from poetry.publishing.uploader import UploadError


if TYPE_CHECKING:
Expand Down Expand Up @@ -82,9 +81,7 @@ def request_callback(*_: Any, **__: Any) -> None:

assert exit_code == 1

expected = str(UploadError(error=requests.ConnectionError()))

assert expected in app_tester.io.fetch_error()
assert "Error connecting to repository" in app_tester.io.fetch_error()


def test_publish_with_cert(
Expand Down

0 comments on commit 64109a7

Please sign in to comment.