Merge branch 'master' of /home/git/wammu
[wammu.git] / Wammu / Error.py
blobfbd823ae1a887fd82758b6106fcac5f179c81238
1 # -*- coding: UTF-8 -*-
2 # vim: expandtab sw=4 ts=4 sts=4:
3 '''
4 Wammu - Phone manager
5 Unexpected exception handler
6 '''
7 __author__ = 'Michal Čihař'
8 __email__ = 'michal@cihar.com'
9 __license__ = '''
10 Copyright © 2003 - 2009 Michal Čihař
12 This program is free software; you can redistribute it and/or modify it
13 under the terms of the GNU General Public License version 2 as published by
14 the Free Software Foundation.
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
19 more details.
21 You should have received a copy of the GNU General Public License along with
22 this program; if not, write to the Free Software Foundation, Inc.,
23 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 '''
26 import traceback
27 try:
28 from hashlib import md5 as md5
29 except ImportError:
30 from md5 import new as md5
31 import Wammu.ErrorLog
32 import Wammu.ErrorMessage
33 from Wammu.Locales import StrConv
35 # set later in Wammu.App to have correct parent here
36 HANDLER_PARENT = None
38 ERROR_HISTORY = []
40 def Handler(errtype, value, tback):
41 """User friendly error handling """
43 # prepare traceback text
44 trace = traceback.extract_tb(tback)
45 linetrace = traceback.format_list(trace)
46 texttrace = ''.join(linetrace)
47 textexc = ''.join(traceback.format_exception_only(errtype, value))
49 # debug log information
50 logtext = ''
51 outf, logname = Wammu.ErrorLog.SaveLog()
52 if outf is not None:
53 print 'Created debug log copy in %s for error reporting.' % logname
54 logtext = '\n%s\n' % _('Debug log was saved for phone communication, if this error appeared during communicating with phone, you are strongly encouraged to include it in bugreport. Debug log is saved in file %s.') % logname
57 # detection of same errors
58 tracehash = md5('%s,%s' % (textexc, texttrace)).hexdigest()
59 if tracehash in ERROR_HISTORY:
60 print 'Same error already detected, not showing dialog!'
61 print texttrace
62 print 'Exception: %s' % textexc
63 return
64 ERROR_HISTORY.append(tracehash)
66 # traceback id (md5 sum of last topmost traceback item inside Wammu - file(function):code)
67 try:
68 for trace_line in trace:
69 if trace_line[0].rfind('Wammu') > -1:
70 lasttrace = trace_line
71 traceidtext = '%s(%s):%s' % (
72 lasttrace[0][lasttrace[0].rfind('Wammu'):],
73 lasttrace[2],
74 lasttrace[3])
75 traceid = md5(traceidtext).hexdigest()
76 tracetext = '\n%s\n' % (
77 _('Before submiting please try searching for simmilar bugs on %s')
78 % ('http://bugs.cihar.com/view_all_set.php?f=3&type=1&search=%s\n'
79 % traceid))
80 except:
81 traceid = 'N/A'
82 tracetext = ''
84 # unicode warning
85 if errtype == UnicodeEncodeError or errtype == UnicodeDecodeError:
86 unicodewarning = ('\n%s\n' %
87 _('Unicode encoding error appeared, see question 1 in FAQ, how to solve this.'))
88 else:
89 unicodewarning = ''
91 # prepare message
92 text = u"""%s
95 %s%s%s
97 ------------------ Traceback ID -------------------
99 -------------------- Traceback --------------------
100 %s-------------------- Exception --------------------
101 %s---------------------------------------------------
102 """ % (
103 _('Unhandled exception appeared.'),
104 _('If you want to help improving this program, please submit following infomation and description how did it happen to %s. Please report in english, otherwise you will be most likely told to translate you report to english later.') % 'http://bugs.wammu.eu/',
105 logtext,
106 tracetext,
107 unicodewarning,
108 Wammu.ErrorLog.GetSystemInfo(),
109 traceid,
110 StrConv(texttrace),
111 StrConv(textexc))
113 # Include exception info in crash file
114 if outf is not None:
115 outf.write(text.encode('utf-8'))
116 outf.close()
118 # display error
119 try:
120 Wammu.ErrorMessage.ErrorMessage(HANDLER_PARENT,
121 _('Unhandled exception appeared. If you want to help improving this program, please report this together with description how this situation has happened. Please report in english, otherwise you will be most likely told to translate you report to english later.'),
122 _('Unhandled exception'),
123 traceid = traceid, autolog = logname,
124 exception = _('Traceback:\n%(traceback)s\nException: %(exception)s') % {
125 'traceback': StrConv(texttrace),
126 'exception' : StrConv(textexc) }).ShowModal()
127 except:
128 print text