Skip to content

Instantly share code, notes, and snippets.

@aredden
Last active June 19, 2022 18:33
Show Gist options
  • Save aredden/b8bcc6c8e3dd2b2de90e27e7a22fc9e0 to your computer and use it in GitHub Desktop.
Save aredden/b8bcc6c8e3dd2b2de90e27e7a22fc9e0 to your computer and use it in GitHub Desktop.
proper python error formatting without necessary non-zero exit code.
from traceback import format_exception
def format_error(e: Exception, limit: int = 3):
"""Format exception as a string.
Args:
e (Exception): Raised exception object.
limit (int): limit of error call stack lines to be included in the formatted string.
"""
e_fmt = format_exception(type(e), e, tb=e.__traceback__, limit=limit)
return "\n".join(e_fmt)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment