Merge translation update.
[wammu.git] / Wammu / Error.py
blobf6573f39f9802237b7ce4747e2e220073f206e41
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 - 2008 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 import md5
28 import Wammu.ErrorLog
29 import Wammu.ErrorMessage
30 from Wammu.Locales import StrConv
32 # set later in Wammu.App to have correct parent here
33 HANDLER_PARENT = None
35 ERROR_HISTORY = []
37 def Handler(errtype, value, tback):
38 """User friendly error handling """
40 # prepare traceback text
41 trace = traceback.extract_tb(tback)
42 linetrace = traceback.format_list(trace)
43 texttrace = ''.join(linetrace)
44 textexc = ''.join(traceback.format_exception_only(errtype, value))
46 # debug log information
47 logtext = ''
48 outf, logname = Wammu.ErrorLog.SaveLog()
49 if outf is not None:
50 print 'Created debug log copy in %s for error reporting.' % logname
51 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
54 # detection of same errors
55 tracehash = md5.new('%s,%s' % (textexc, texttrace)).hexdigest()
56 if tracehash in ERROR_HISTORY:
57 print 'Same error already detected, not showing dialog!'
58 print texttrace
59 print 'Exception: %s' % textexc
60 return
61 ERROR_HISTORY.append(tracehash)
63 # traceback id (md5 sum of last topmost traceback item inside Wammu - file(function):code)
64 try:
65 for trace_line in trace:
66 if trace_line[0].rfind('Wammu') > -1:
67 lasttrace = trace_line
68 traceidtext = '%s(%s):%s' % (
69 lasttrace[0][lasttrace[0].rfind('Wammu'):],
70 lasttrace[2],
71 lasttrace[3])
72 traceid = md5.new(traceidtext).hexdigest()
73 tracetext = '\n%s\n' % (
74 _('Before submiting please try searching for simmilar bugs on %s')
75 % ('http://bugs.cihar.com/view_all_set.php?f=3&type=1&search=%s\n'
76 % traceid))
77 except:
78 traceid = 'N/A'
79 tracetext = ''
81 # unicode warning
82 if errtype == UnicodeEncodeError or errtype == UnicodeDecodeError:
83 unicodewarning = ('\n%s\n' %
84 _('Unicode encoding error appeared, see question 1 in FAQ, how to solve this.'))
85 else:
86 unicodewarning = ''
88 # prepare message
89 text = u"""%s
92 %s%s%s
94 ------------------ Traceback ID -------------------
96 -------------------- Traceback --------------------
97 %s-------------------- Exception --------------------
98 %s---------------------------------------------------
99 """ % (
100 _('Unhandled exception appeared.'),
101 _('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/',
102 logtext,
103 tracetext,
104 unicodewarning,
105 Wammu.ErrorLog.GetSystemInfo(),
106 traceid,
107 StrConv(texttrace),
108 StrConv(textexc))
110 # Include exception info in crash file
111 if outf is not None:
112 outf.write(text.encode('utf-8'))
113 outf.close()
115 # display error
116 try:
117 Wammu.ErrorMessage.ErrorMessage(HANDLER_PARENT,
118 _('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.'),
119 _('Unhandled exception'),
120 traceid = traceid, autolog = logname,
121 exception = _('Traceback:\n%(traceback)s\nException: %(exception)s') % {
122 'traceback': StrConv(texttrace),
123 'exception' : StrConv(textexc) }).ShowModal()
124 except:
125 print text