Skip to content

Commit eaaae0b

Browse files
committed
added terminal handler
1 parent 06f7e37 commit eaaae0b

File tree

5 files changed

+57
-5
lines changed

5 files changed

+57
-5
lines changed

app/http/controllers/PackageController.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from masonite.request import Request
55
from app.User import User
66
from src.exceptionite.errors import Handler
7+
from src.exceptionite.errors.TerminalHandler import TerminalHandler
78
from masonite.exceptions import ContainerError, InvalidCSRFToken
89
import pickle
910

@@ -26,11 +27,13 @@ def show(self, view: View, request: Request):
2627
try:
2728
Throw(view)
2829
except Exception as e:
29-
exception = Handler(e)
30+
pass
31+
# exception = TerminalHandler(e)
3032

3133
dic = {}
34+
35+
# dic.update(['hello', 'world'])
3236
Fun()()
33-
dic.update(['hello', 'world'])
3437

3538
return view.render('woh')
3639

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
masonite>=2.2,<2.3
1+
masonite>=2.3,<2.4
22
flake8
33
pytest

src/exceptionite/errors/Handler.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414

1515
from .StackOverflowIntegration import StackOverflowIntegration
1616

17-
1817
class Handler:
1918

2019
def __init__(self, e=False, **kwargs):
2120
self.e = e
21+
2222
self._contexts = {}
2323
self._integrations = {}
2424

@@ -99,6 +99,11 @@ def create_trace(self):
9999
for tb in self.traceback.stack:
100100
traceback.append(StackLine(tb, variables=tb.locals))
101101

102+
if hasattr(self.e, 'from_obj'):
103+
frame_summary = [inspect.getsourcefile(
104+
self.e.from_obj), inspect.getsourcelines(self.e.from_obj)[1], '', 'null']
105+
traceback.append(StackLine(frame_summary, variables={}))
106+
102107
return traceback
103108

104109
def render(self):
@@ -122,7 +127,8 @@ def render(self):
122127
loader=loader,
123128
autoescape=select_autoescape(['html', 'xml'])
124129
)
125-
130+
from .TerminalHandler import TerminalHandler
131+
TerminalHandler(self.e).render()
126132
return environment.get_template('exception.html').render({'exception': self})
127133

128134
@staticmethod
@@ -201,6 +207,7 @@ def __init__(self, frame_summary, variables={}):
201207
formatted_line = (content
202208
.replace("'\n '", '')
203209
.replace('\'\n "', ''))
210+
204211
if self.statement in formatted_line:
205212
self.offending_line = read_line
206213

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from .Handler import Handler
2+
from tabulate import tabulate
3+
from colorama import init
4+
from colorama import Fore, Back, Style
5+
6+
7+
class TerminalHandler(Handler):
8+
9+
10+
def render(self):
11+
print('')
12+
table = []
13+
print('')
14+
print(' ', self.exception(), '-', self.message())
15+
print('')
16+
init()
17+
# print(' ', 'at', stack.file, 'on line', stack.lineno)
18+
first_stack = self.stacktrace()[0]
19+
for line in first_stack.file_contents.items():
20+
lineno, file_line = line
21+
table.append(["lineno", lineno])
22+
23+
if lineno == first_stack.offending_line:
24+
print(Fore.RED + Style.BRIGHT, '', "", '>', str(lineno), '|', file_line.replace('&nbsp;', ' '), '\033[0m')
25+
else:
26+
print(' ', ' ', str(lineno), '|', file_line.replace('&nbsp;', ' '))
27+
print('')
28+
print('Stack Trace:')
29+
print('')
30+
for index, stack in enumerate(self.stacktrace()):
31+
print(' ', "# " + str(index + 1), stack.file, "on line", stack.lineno)
32+

tests/test_terminal_handler.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import unittest
2+
from src.exceptionite.errors.TerminalHandler import TerminalHandler
3+
4+
class TestTerminalHandler(unittest.TestCase):
5+
6+
def test_outputs_handler(self):
7+
try:
8+
2 / 0
9+
except Exception as e:
10+
TerminalHandler(e).render()

0 commit comments

Comments
 (0)