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 3 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
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
25 changes: 25 additions & 0 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import sys

import colorama
import pytest

from twine import __main__ as dunder_main
from twine import cli


Expand Down Expand Up @@ -46,6 +48,29 @@ 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 = (
"HTTPError from https://test.pypi.org/legacy/: 403 Client Error\n"
"Invalid or non-existent authentication information. "
"See https://test.pypi.org/help/#invalid-auth for details"
)
bhrutledge marked this conversation as resolved.
Show resolved Hide resolved

assert dunder_main.main() == colorama.Fore.RED + message + colorama.Style.RESET_ALL


@pytest.mark.xfail(
sys.platform == "win32",
reason="pytest-services watcher_getter fixture does not support Windows",
Expand Down
24 changes: 22 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 re
import sys
from typing import Any

Expand All @@ -25,8 +26,27 @@
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:
# '%s Client Error: %s for url: %s'
# '%s Server Error: %s for url: %s'
pattern = r"(?P<status>.*Error): (?P<reason>.*) for url: (?P<url>.*)"
match = re.match(pattern, exc.args[0])
bhrutledge marked this conversation as resolved.
Show resolved Hide resolved
if match:
return (
f"{exc.__class__.__name__} from {match['url']}: {match['status']}\n"
f"{match['reason']}"
)
return _format_exception(exc)


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


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