gristlabs_grist-core/sandbox/grist/friendly_errors.py
Alex Hall 49cb51bac5 (core) Error explanations from friendly-traceback
Summary: Extend formula error messages with explanations from https://github.com/friendly-traceback/friendly-traceback. Only for Python 3.

Test Plan: Updated several Python tests. In general, these require separate branches for Python 2 and 3.

Reviewers: georgegevoian

Reviewed By: georgegevoian

Differential Revision: https://phab.getgrist.com/D3542
2022-08-12 19:45:00 +02:00

44 lines
1.4 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 ""
# 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 ""