feat: 🎸 Add prettify_exc method to handle known errors

catch known errors in stderr and display them correctly
This commit is contained in:
Adam Goldschmidt
2019-03-11 09:35:17 +02:00
parent f52abe32bc
commit 2d75f1af53
2 changed files with 21 additions and 9 deletions
+2 -9
View File
@@ -926,16 +926,9 @@ def do_create_virtualenv(python=None, site_packages=False, pypi_mirror=None):
click.echo(crayons.blue("{0}".format(c.out)), err=True)
if c.returncode != 0:
sp.fail(environments.PIPENV_SPINNER_FAIL_TEXT.format("Failed creating virtual environment"))
known_exceptions = {
"PermissionError": "Permission denied:",
}
# PermissionError - hide the traceback for better UX
for partition in (part
for e, part in known_exceptions.items() if e in c.err):
known_exceptions_partition = c.err.rpartition(partition)
c.err = "{} {}".format(known_exceptions_partition[1], known_exceptions_partition[2])
error = c.err if environments.is_verbose() else exceptions.prettify_exc(c.err)
raise exceptions.VirtualenvCreationException(
extra=[crayons.red("{0}".format(c.err)),]
extra=[crayons.red("{0}".format(error)),]
)
else:
+19
View File
@@ -18,7 +18,11 @@ from .vendor.click.exceptions import (
)
from .vendor.click.types import Path
from .vendor.click.utils import echo as click_echo
import vistir
KNOWN_EXCEPTIONS = {
"PermissionError": "Permission denied:",
}
def handle_exception(exc_type, exception, traceback, hook=sys.excepthook):
if environments.is_verbose() or not issubclass(exc_type, ClickException):
@@ -402,3 +406,18 @@ class RequirementError(PipenvException):
)
extra = [crayons.normal(decode_for_output(str(req)))]
super(RequirementError, self).__init__(message, extra=extra)
super(ResolutionFailure, self).__init__(fix_utf8(message), extra=extra)
def prettify_exc(error):
"""Catch known errors and prettify them instead of showing the
entire traceback, for better UX"""
matched_exceptions = [k for k in KNOWN_EXCEPTIONS.keys() if k in error]
if not matched_exceptions:
return "{}".format(vistir.misc.decode_for_output(error))
errors = []
for match in matched_exceptions:
_, error, info = error.rpartition(KNOWN_EXCEPTIONS[match])
errors.append("{} {}".format(error, info))
return "\n".join(errors)