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

Reformat HTTP errors #666

Merged
merged 10 commits into from
Jun 22, 2020
Merged
Show file tree
Hide file tree
Changes from 7 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
8 changes: 0 additions & 8 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,5 @@ exclude_lines =
# Don't complain if non-runnable code isn't run
if __name__ == .__main__.:


# Paths to omit from consideration
omit =
# __main__.py exists only as a very basic wrapper around warehouse.cli
# and exists only to provide setuptools and python -m a place to point
# at.
*/twine/__main__.py

bhrutledge marked this conversation as resolved.
Show resolved Hide resolved
[html]
show_contexts = True
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def entered_password(monkeypatch):
monkeypatch.setattr(getpass, "getpass", lambda prompt: "entered pw")


@pytest.fixture
@pytest.fixture(scope="session")
def sampleproject_dist(tmp_path_factory):
checkout = tmp_path_factory.mktemp("sampleproject", numbered=False)
Comment on lines -131 to 133
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quick fix to avoid a "file exists" error from mktemp when this fixture is used in more than one test.

subprocess.run(
Expand Down
28 changes: 28 additions & 0 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import re
import sys

import colorama
import pytest

from twine import __main__ as dunder_main
from twine import cli


Expand Down Expand Up @@ -46,6 +49,31 @@ def test_pypi_upload(sampleproject_dist):
cli.dispatch(command)


def test_pypi_error(sampleproject_dist, monkeypatch):
command = [
"twine",
"upload",
"--repository-url",
"https://test.pypi.org/legacy/",
"--username",
"foo",
"--password",
"bar",
str(sampleproject_dist),
]
monkeypatch.setattr(sys, "argv", command)

message = (
re.escape(colorama.Fore.RED)
+ r"HTTPError from https://test.pypi.org/legacy/: 403 Forbidden\n"
+ r".*authentication"
)
bhrutledge marked this conversation as resolved.
Show resolved Hide resolved

result = dunder_main.main()

assert re.match(message, result)


@pytest.mark.xfail(
sys.platform == "win32",
reason="pytest-services watcher_getter fixture does not support Windows",
Expand Down
21 changes: 19 additions & 2 deletions twine/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import http
import sys
from typing import Any

Expand All @@ -25,8 +26,24 @@
def main() -> Any:
try:
return cli.dispatch(sys.argv[1:])
except (exceptions.TwineException, requests.HTTPError) as exc:
return _format_error(f"{exc.__class__.__name__}: {exc.args[0]}")
except requests.HTTPError as exc:
return _format_error(_format_http_exception(exc))
except exceptions.TwineException as exc:
return _format_error(_format_exception(exc))


def _format_http_exception(exc: requests.HTTPError) -> str:
status_code = exc.response.status_code
status_phrase = http.HTTPStatus(status_code).phrase
return (
f"{exc.__class__.__name__} from {exc.response.url}: "
f"{status_code} {status_phrase}\n"
f"{exc.response.reason}"
)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.



def _format_exception(exc: Exception) -> str:
return f"{exc.__class__.__name__}: {exc.args[0]}"


def _format_error(message: str) -> str:
Expand Down