""" Exception related utilities """ import json import traceback def get_error_msg(error: Exception) -> str: """ Get error message (with stack trace) from an exception into a single json-serializable string. This is useful for logging to a single line in the logs, which allows easy identification the associated exception, rather than wondering which part of the logs belong to the same exception thrown. """ return json.dumps("".join(traceback.format_exception(type(error), error, error.__traceback__)))