-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Error.py
134 lines (120 loc) · 4.72 KB
/
Error.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# -*- coding: UTF-8 -*-
#
# Copyright © 2003 - 2018 Michal Čihař <michal@cihar.com>
#
# This file is part of Wammu <https://wammu.eu/>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
'''
Wammu - Phone manager
Unexpected exception handler
'''
import traceback
try:
from hashlib import md5 as md5
except ImportError:
from md5 import new as md5
import Wammu.ErrorLog
import Wammu.ErrorMessage
from Wammu.Locales import StrConv
from Wammu.Locales import ugettext as _
# set later in Wammu.App to have correct parent here
HANDLER_PARENT = None
ERROR_HISTORY = []
def Handler(errtype, value, tback):
"""User friendly error handling """
# prepare traceback text
trace = traceback.extract_tb(tback)
linetrace = traceback.format_list(trace)
texttrace = ''.join(linetrace)
textexc = ''.join(traceback.format_exception_only(errtype, value))
# debug log information
logtext = ''
outf, logname = Wammu.ErrorLog.SaveLog()
if outf is not None:
print('Created debug log copy in %s for error reporting.' % logname)
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
# detection of same errors
tracehash = md5('%s,%s' % (textexc, texttrace)).hexdigest()
if tracehash in ERROR_HISTORY:
print('Same error already detected, not showing dialog!')
print(texttrace)
print('Exception: %s' % textexc)
return
ERROR_HISTORY.append(tracehash)
# traceback id (md5 sum of last topmost traceback item inside Wammu - file(function):code)
try:
for trace_line in trace:
if trace_line[0].rfind('Wammu') > -1:
lasttrace = trace_line
traceidtext = '%s(%s):%s' % (
lasttrace[0][lasttrace[0].rfind('Wammu'):],
lasttrace[2],
lasttrace[3])
traceid = md5(traceidtext).hexdigest()
tracetext = '\n%s\n' % (
_('Before submiting please try searching for similar bugs on %s')
% ('https://github.com/search?l=&q=%s+%%40gammu&ref=advsearch&type=Issues'
% traceid))
except:
traceid = 'N/A'
tracetext = ''
# unicode warning
if errtype == UnicodeEncodeError or errtype == UnicodeDecodeError:
unicodewarning = '\n%s\n' % (
_('Unicode encoding error appeared, see question 1 in FAQ, how to solve this.')
)
else:
unicodewarning = ''
# prepare message
text = """%s
%s
%s%s%s
%s
------------------ Traceback ID -------------------
%s
-------------------- Traceback --------------------
%s-------------------- Exception --------------------
%s---------------------------------------------------
""" % (
_('Unhandled exception appeared.'),
_('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.') % 'https://github.com/gammu/wammu/issues/',
logtext,
tracetext,
unicodewarning,
Wammu.ErrorLog.GetSystemInfo(),
traceid,
StrConv(texttrace),
StrConv(textexc)
)
# Include exception info in crash file
if outf is not None:
outf.write(text.encode('utf-8'))
outf.close()
# display error
try:
Wammu.ErrorMessage.ErrorMessage(
HANDLER_PARENT,
_('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.'),
_('Unhandled exception'),
traceid=traceid,
autolog=logname,
exception=_('Traceback:\n%(traceback)s\nException: %(exception)s') % {
'traceback': StrConv(texttrace),
'exception': StrConv(textexc)
}
).ShowModal()
except:
print(text)