Skip to content

Commit 6c62d23

Browse files
authored
Merge pull request #3463 from AdamGold/bugfix/catch-command-errors
conservative check for known exceptions in subprocess stderr.
2 parents 8aa9283 + 2d75f1a commit 6c62d23

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

news/2553.behavior.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Make conservative checks of known exceptions when subprocess returns output, so user won't see the whole traceback - just the error.

pipenv/core.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -925,9 +925,10 @@ def do_create_virtualenv(python=None, site_packages=False, pypi_mirror=None):
925925
)
926926
click.echo(crayons.blue("{0}".format(c.out)), err=True)
927927
if c.returncode != 0:
928-
sp.fail(environments.PIPENV_SPINNER_FAIL_TEXT.format(u"Failed creating virtual environment"))
928+
sp.fail(environments.PIPENV_SPINNER_FAIL_TEXT.format("Failed creating virtual environment"))
929+
error = c.err if environments.is_verbose() else exceptions.prettify_exc(c.err)
929930
raise exceptions.VirtualenvCreationException(
930-
extra=[crayons.blue("{0}".format(c.err)),]
931+
extra=[crayons.red("{0}".format(error)),]
931932
)
932933
else:
933934

pipenv/exceptions.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@
1818
)
1919
from .vendor.click.types import Path
2020
from .vendor.click.utils import echo as click_echo
21+
import vistir
2122

23+
KNOWN_EXCEPTIONS = {
24+
"PermissionError": "Permission denied:",
25+
}
2226

2327
def handle_exception(exc_type, exception, traceback, hook=sys.excepthook):
2428
if environments.is_verbose() or not issubclass(exc_type, ClickException):
@@ -402,3 +406,18 @@ def __init__(self, req=None):
402406
)
403407
extra = [crayons.normal(decode_for_output(str(req)))]
404408
super(RequirementError, self).__init__(message, extra=extra)
409+
super(ResolutionFailure, self).__init__(fix_utf8(message), extra=extra)
410+
411+
412+
def prettify_exc(error):
413+
"""Catch known errors and prettify them instead of showing the
414+
entire traceback, for better UX"""
415+
matched_exceptions = [k for k in KNOWN_EXCEPTIONS.keys() if k in error]
416+
if not matched_exceptions:
417+
return "{}".format(vistir.misc.decode_for_output(error))
418+
errors = []
419+
for match in matched_exceptions:
420+
_, error, info = error.rpartition(KNOWN_EXCEPTIONS[match])
421+
errors.append("{} {}".format(error, info))
422+
423+
return "\n".join(errors)

0 commit comments

Comments
 (0)