gristlabs_grist-core/sandbox/grist/friendly_errors.py
Alex Hall 16a43edc2e (core) Upgrade friendly-traceback and its dependencies
Summary:
Upgrading the friendly-traceback package to include a fix that I specifically requested in https://github.com/friendly-traceback/friendly-traceback/issues/144 as a solution for the problem mentioned in https://grist.quip.com/HoSmAlvFax0j#MbTADAEcJb7 . Specifically, this shows a friendly explanation when using `len()` with a generator expression.

Also upgraded the dependencies `executing` and `stack_data` (which are mine) while I'm at it, although I don't expect this to really change anything.

Test Plan:
Existing tests. There was one test failure because of a new explanation about generic `Exception`s which I've suppressed.

Tested manually that the new explanation appears:

{F64605}

Reviewers: dsagal

Reviewed By: dsagal

Differential Revision: https://phab.getgrist.com/D3687
2022-10-28 14:44:14 +02:00

48 lines
1.6 KiB
Python

def friendly_message(exc):
"""
Returns a string to append to a standard error message.
If possible, the string contains a friendly explanation of the error.
Otherwise, the string is empty.
"""
try:
if "has no column" in str(exc):
# Avoid the standard AttributeError explanation
return ""
# Imported locally because it's Python 3 only
from friendly_traceback.core import FriendlyTraceback
fr = FriendlyTraceback(type(exc), exc, exc.__traceback__)
fr.assign_generic()
fr.assign_cause()
generic = fr.info["generic"] # broad explanation for the exception class
cause = fr.info.get("cause") # more specific explanation
if "https://github.com" in generic:
# This is a placeholder message when there is no explanation,
# with a suggestion to report the case on GitHub.
return ""
if "All built-in exceptions defined by Python are derived from `Exception`" in generic:
# Unhelpful explanation for a generic `Exception`
return ""
# Add a blank line between the standard message and the friendly message
result = "\n\n" + generic
# Check for the placeholder message again in the cause
if cause and "https://github.com" not in cause:
result += "\n" + cause
result = result.rstrip()
if isinstance(exc, SyntaxError):
result += "\n\n"
return result
except (Exception, SystemExit):
# This can go wrong in many ways, it's not worth propagating the error.
# friendly-traceback raises SystemExit when it encounters an internal error.
# Note that SystemExit is not a subclass of Exception.
return ""