beremiz

fix exception handling

2017-01-27, Surkov Sergey
2d1fb99065e8
Parents 91b7cc4b0d44
Children 13d15a1ae899
fix exception handling
now only one exception dialog is displayed, if many same type exceptions handled (like in #16), and bug reports files always save after exception, not only after pressing "OK" button in exception dialog, closes #17
  • +36 -38
    Beremiz.py
  • --- a/Beremiz.py Fri Jan 27 13:04:25 2017 +0300
    +++ b/Beremiz.py Fri Jan 27 16:01:23 2017 +0300
    @@ -1223,50 +1223,48 @@
    def AddExceptHook(path, app_version='[No version]'):#, ignored_exceptions=[]):
    + def save_bug_report(e_type, e_value, e_traceback, bug_report_path,date):
    + info = {
    + 'app-title': wx.GetApp().GetAppName(), # app_title
    + 'app-version': app_version,
    + 'wx-version': wx.VERSION_STRING,
    + 'wx-platform': wx.Platform,
    + 'python-version': platform.python_version(), # sys.version.split()[0],
    + 'platform': platform.platform(),
    + 'e-type': e_type,
    + 'e-value': e_value,
    + 'date': date,
    + 'cwd': os.getcwd(),
    + }
    + if e_traceback:
    + info['traceback'] = ''.join(traceback.format_tb(e_traceback)) + '%s: %s' % (e_type, e_value)
    + last_tb = get_last_traceback(e_traceback)
    + exception_locals = last_tb.tb_frame.f_locals # the locals at the level of the stack trace where the exception actually occurred
    + info['locals'] = format_namespace(exception_locals)
    + if 'self' in exception_locals:
    + try:
    + info['self'] = format_namespace(exception_locals['self'].__dict__)
    + except:
    + pass
    + if not os.path.exists(path):
    + os.mkdir(path)
    + output = open(bug_report_path, 'w')
    + lst = info.keys()
    + lst.sort()
    + for a in lst:
    + output.write(a + ":\n" + str(info[a]) + "\n\n")
    + output.close()
    +
    def handle_exception(e_type, e_value, e_traceback):
    traceback.print_exception(e_type, e_value, e_traceback) # this is very helpful when there's an exception in the rest of this func
    last_tb = get_last_traceback(e_traceback)
    ex = (last_tb.tb_frame.f_code.co_filename, last_tb.tb_frame.f_lineno)
    if ex not in ignored_exceptions:
    + ignored_exceptions.append(ex)
    date = time.ctime()
    - try:
    - os.mkdir(path)
    - except OSError:
    - pass
    - bug_report_path = path+os.sep+"bug_report_"+date.replace(':','-').replace(' ','_')+".txt"
    - result = Display_Exception_Dialog(e_type,e_value,e_traceback,bug_report_path)
    - if result:
    - ignored_exceptions.append(ex)
    - info = {
    - 'app-title' : wx.GetApp().GetAppName(), # app_title
    - 'app-version' : app_version,
    - 'wx-version' : wx.VERSION_STRING,
    - 'wx-platform' : wx.Platform,
    - 'python-version' : platform.python_version(), #sys.version.split()[0],
    - 'platform' : platform.platform(),
    - 'e-type' : e_type,
    - 'e-value' : e_value,
    - 'date' : date,
    - 'cwd' : os.getcwd(),
    - }
    - if e_traceback:
    - info['traceback'] = ''.join(traceback.format_tb(e_traceback)) + '%s: %s' % (e_type, e_value)
    - last_tb = get_last_traceback(e_traceback)
    - exception_locals = last_tb.tb_frame.f_locals # the locals at the level of the stack trace where the exception actually occurred
    - info['locals'] = format_namespace(exception_locals)
    - if 'self' in exception_locals:
    - try :
    - info['self'] = format_namespace(exception_locals['self'].__dict__)
    - except :
    - pass
    -
    - output = open(bug_report_path,'w')
    - lst = info.keys()
    - lst.sort()
    - for a in lst:
    - output.write(a+":\n"+str(info[a])+"\n\n")
    - output.close()
    -
    + bug_report_path = path + os.sep + "bug_report_" + date.replace(':', '-').replace(' ', '_') + ".txt"
    + save_bug_report(e_type, e_value, e_traceback, bug_report_path, date)
    + Display_Exception_Dialog(e_type, e_value, e_traceback, bug_report_path)
    #sys.excepthook = lambda *args: wx.CallAfter(handle_exception, *args)
    sys.excepthook = handle_exception