Use cgitb instead of DIY traceback

This commit is contained in:
Jeong YunWon 2017-03-05 01:20:34 +09:00
parent f9dc34c8fa
commit 51e9d67562
2 changed files with 11 additions and 18 deletions

View File

@ -77,16 +77,6 @@ TRACEBACK_WRAPPER_HTML = '''
</html> </html>
''' '''
TRACEBACK_LINE_HTML = '''
<div class="frame-line">
<p class="frame-descriptor">
File {0.filename}, line <i>{0.lineno}</i>,
in <code><b>{0.name}</b></code>
</p>
<p class="frame-code"><code>{0.line}</code></p>
</div>
'''
INTERNAL_SERVER_ERROR_HTML = ''' INTERNAL_SERVER_ERROR_HTML = '''
<h1>Internal Server Error</h1> <h1>Internal Server Error</h1>
<p> <p>

View File

@ -1,5 +1,7 @@
import sys import sys
from traceback import format_exc, extract_tb from traceback import format_exc, extract_tb
import cgitb
from io import StringIO
from sanic.exceptions import ( from sanic.exceptions import (
ContentRangeError, ContentRangeError,
@ -7,13 +9,19 @@ from sanic.exceptions import (
INTERNAL_SERVER_ERROR_HTML, INTERNAL_SERVER_ERROR_HTML,
InvalidRangeType, InvalidRangeType,
SanicException, SanicException,
TRACEBACK_LINE_HTML,
TRACEBACK_STYLE, TRACEBACK_STYLE,
TRACEBACK_WRAPPER_HTML) TRACEBACK_WRAPPER_HTML)
from sanic.log import log from sanic.log import log
from sanic.response import text, html from sanic.response import text, html
def format_cgitb(format, info=None):
s = StringIO()
cgitb.Hook(file=s, format=format).handle()
tb = s.getvalue()
return tb
class ErrorHandler: class ErrorHandler:
handlers = None handlers = None
@ -23,17 +31,12 @@ class ErrorHandler:
def _render_traceback_html(self, exception, request): def _render_traceback_html(self, exception, request):
exc_type, exc_value, tb = sys.exc_info() exc_type, exc_value, tb = sys.exc_info()
frames = extract_tb(tb) frame_html = format_cgitb('html')
frame_html = []
for frame in frames:
frame_html.append(TRACEBACK_LINE_HTML.format(frame))
return TRACEBACK_WRAPPER_HTML.format( return TRACEBACK_WRAPPER_HTML.format(
style=TRACEBACK_STYLE, style=TRACEBACK_STYLE,
exc_name=exc_type.__name__, exc_name=exc_type.__name__,
exc_value=exc_value, exc_value=exc_value,
frame_html=''.join(frame_html), frame_html=frame_html,
uri=request.url) uri=request.url)
def add(self, exception, handler): def add(self, exception, handler):