-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathexceptionHandler.py
More file actions
69 lines (65 loc) · 2.24 KB
/
exceptionHandler.py
File metadata and controls
69 lines (65 loc) · 2.24 KB
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
import sys
import traceback
import re
from dataclasses import dataclass
# local imports
from constants import *
import stacktrace
import paths
from myLogging import *
import errors
import utils
_tbPattern = re.compile(r'(\s*File\s+")([^"]+)(".*)')
def _rewriteFilenameInTracebackLine(s: str) -> str:
# Match the pattern: File "filename", line number, in function
match = re.match(_tbPattern, s)
if match:
prefix = match.group(1)
filename = match.group(2)
suffix = match.group(3)
canonicalized = paths.canonicalizePath(filename)
return prefix + canonicalized + suffix
else:
return s
def _rewriteFilenameInTraceback(s: str) -> str:
lines = s.split('\n')
result = []
for l in lines:
result.append(_rewriteFilenameInTracebackLine(l))
return '\n'.join(result)
def handleCurrentException(exit=True, removeFirstTb=False, file=sys.stderr):
(etype, val, tb) = sys.exc_info()
if isinstance(val, SystemExit):
utils.die(val.code)
isWyppError = isinstance(val, errors.WyppError)
if isWyppError:
extra = val.extraFrames
else:
extra = []
frameList = (stacktrace.tbToFrameList(tb) if tb is not None else [])
if frameList and removeFirstTb:
frameList = frameList[1:]
isSyntaxError = isinstance(val, SyntaxError)
lastFrameIsWypp = len(frameList) > 0 and stacktrace.isWyppFrame(frameList[-1])
isBug = not isWyppError and not isSyntaxError and lastFrameIsWypp
stackSummary = stacktrace.limitTraceback(frameList, extra, not isBug and not isDebug())
header = False
for x in stackSummary.format():
if not header:
file.write('Traceback (most recent call last):\n')
header = True
file.write(_rewriteFilenameInTraceback(x))
if isWyppError:
s = str(val)
if s and s[0] != '\n':
file.write('\n')
file.write(s)
file.write('\n')
else:
for x in traceback.format_exception_only(etype, val):
file.write(x)
if isBug:
debug(f'isWyppError={isWyppError}, isSyntaxError={isSyntaxError}, lastFrameIsWypp={lastFrameIsWypp}')
file.write(f'BUG: the error above is most likely a bug in WYPP!')
if exit:
utils.die(1)