def wrap_exceptions(func):
"""Wraps a function with an "exception hook" for threads.
Args:
func (function): The function to wrap.
Returns:
function: The wrapped function
"""
@wraps(func)
def wrapper(*args, **kwargs):
# args[0] = when wrapping a class method. (IT BETTER BE. WHYDOISUCKATPROGRAMMINGOHGOD)
# This should really only be used on threads (main thread has a sys.excepthook)
try:
return func(*args, **kwargs)
except (SystemExit, KeyboardInterrupt):
raise
except Exception:
if isinstance(args[0], wx.TopLevelWindow):
parent = args[0]
elif hasattr(args[0], "parent") and isinstance(args[0].parent, wx.TopLevelWindow):
parent = args[0].parent
else:
parent = wx.GetApp().GetTopWindow()
error_message = ''.join(traceback.format_exc())
error_dialog = wx.MessageDialog(parent=parent,
message="An error has occured\n\n" + error_message,
caption="Error!", style=wx.OK | wx.ICON_ERROR)
error_dialog.RequestUserAttention()
error_dialog.ShowModal()
error_dialog.Destroy()
logger.critical(error_message)
raise
return wrapper
评论列表
文章目录