From 51e9d675621b0d8a4be194119b381504781a425b Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Sun, 5 Mar 2017 01:20:34 +0900 Subject: [PATCH] Use cgitb instead of DIY traceback --- sanic/exceptions.py | 10 ---------- sanic/handlers.py | 19 +++++++++++-------- 2 files changed, 11 insertions(+), 18 deletions(-) diff --git a/sanic/exceptions.py b/sanic/exceptions.py index 4f01d64e..ae1123f8 100644 --- a/sanic/exceptions.py +++ b/sanic/exceptions.py @@ -77,16 +77,6 @@ TRACEBACK_WRAPPER_HTML = ''' ''' -TRACEBACK_LINE_HTML = ''' -
-

- File {0.filename}, line {0.lineno}, - in {0.name} -

-

{0.line}

-
-''' - INTERNAL_SERVER_ERROR_HTML = '''

Internal Server Error

diff --git a/sanic/handlers.py b/sanic/handlers.py index d5a5d0dd..0d136281 100644 --- a/sanic/handlers.py +++ b/sanic/handlers.py @@ -1,5 +1,7 @@ import sys from traceback import format_exc, extract_tb +import cgitb +from io import StringIO from sanic.exceptions import ( ContentRangeError, @@ -7,13 +9,19 @@ from sanic.exceptions import ( INTERNAL_SERVER_ERROR_HTML, InvalidRangeType, SanicException, - TRACEBACK_LINE_HTML, TRACEBACK_STYLE, TRACEBACK_WRAPPER_HTML) from sanic.log import log 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: handlers = None @@ -23,17 +31,12 @@ class ErrorHandler: def _render_traceback_html(self, exception, request): exc_type, exc_value, tb = sys.exc_info() - frames = extract_tb(tb) - - frame_html = [] - for frame in frames: - frame_html.append(TRACEBACK_LINE_HTML.format(frame)) - + frame_html = format_cgitb('html') return TRACEBACK_WRAPPER_HTML.format( style=TRACEBACK_STYLE, exc_name=exc_type.__name__, exc_value=exc_value, - frame_html=''.join(frame_html), + frame_html=frame_html, uri=request.url) def add(self, exception, handler):