Skip to content
This repository has been archived by the owner on Jul 3, 2023. It is now read-only.

Commit

Permalink
Wraps sanitize_error in try except
Browse files Browse the repository at this point in the history
Since we don't want this code to cause
a cryptic error message for the end user,
so we wrap it in a try except.
  • Loading branch information
skrawcz committed Dec 27, 2022
1 parent f1d44b9 commit 34e574e
Showing 1 changed file with 27 additions and 21 deletions.
48 changes: 27 additions & 21 deletions hamilton/telemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,24 +240,30 @@ def sanitize_error(exc_type, exc_value, exc_traceback) -> str:
:param exc_traceback: pulled from `sys.exc_info()`
:return: string to use for telemetry
"""
import traceback

te = traceback.TracebackException(exc_type, exc_value, exc_traceback)
sanitized_string = ""
for f in te.stack:
s = f.filename.split(os.sep)
# take last 3 places only -- that's how deep hamilton is.
s = s[-3:]
s.reverse()
if "hamilton" not in s:
sanitized_string += "...<USER_CODE>...\n"
continue
file_name = ""
for part in s:
if part == "hamilton":
file_name = ".../hamilton" + file_name
break
else:
file_name = "/" + part + file_name
sanitized_string += f"{file_name}, line {f.lineno}, in {f.name}\n"
return sanitized_string
try:
import traceback

te = traceback.TracebackException(exc_type, exc_value, exc_traceback)
sanitized_string = ""
for f in te.stack:
s = f.filename.split(os.sep)
# take last 3 places only -- that's how deep hamilton is.
s = s[-3:]
s.reverse()
if "hamilton" not in s:
sanitized_string += "...<USER_CODE>...\n"
continue
file_name = ""
for part in s:
if part == "hamilton":
file_name = ".../hamilton" + file_name
break
else:
file_name = "/" + part + file_name
sanitized_string += f"{file_name}, line {f.lineno}, in {f.name}\n"
return sanitized_string
except Exception as e:
# we don't want this to fail
if logger.isEnabledFor(logging.DEBUG):
logger.debug(f"Encountered exception sanitizing error. Got:\n{e}")
return "FAILED_TO_SANITIZE_ERROR"

0 comments on commit 34e574e

Please sign in to comment.