From 909cf4257a8c47df49e79344fbb9f014e58ab061 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Tue, 9 Jul 2019 14:28:24 +0100 Subject: [PATCH 1/3] Tweak exception debug HTML --- starlette/middleware/errors.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/starlette/middleware/errors.py b/starlette/middleware/errors.py index 0d153ac4..547ff075 100644 --- a/starlette/middleware/errors.py +++ b/starlette/middleware/errors.py @@ -52,6 +52,11 @@ p { .collapsed { display: none; } +.source-code { + font-family: courier; + font-size: small; + padding-bottom: 10px; +} """ JS = """ @@ -98,7 +103,7 @@ FRAME_TEMPLATE = """ in {frame_name}

-
{code_context}
+
{code_context}
""" @@ -183,7 +188,7 @@ class ServerErrorMiddleware: return LINE.format(**values) return CENTER_LINE.format(**values) - def generate_frame_html(self, frame: inspect.FrameInfo, center_lineno: int) -> str: + def generate_frame_html(self, frame: inspect.FrameInfo, center_lineno: int, is_collapsed: bool) -> str: code_context = "".join( self.format_line(context_position, line, frame.lineno, center_lineno) for context_position, line in enumerate(frame.code_context) @@ -194,6 +199,7 @@ class ServerErrorMiddleware: "frame_lineno": frame.lineno, "frame_name": frame.function, "code_context": code_context, + "collapsed": "collapsed" if is_collapsed else "" } return FRAME_TEMPLATE.format(**values) @@ -206,9 +212,12 @@ class ServerErrorMiddleware: ) center_lineno = int((limit - 1) / 2) - exc_html = "".join( - self.generate_frame_html(frame, center_lineno) for frame in reversed(frames) - ) + exc_html = "" + is_collapsed = False + for frame in reversed(frames): + exc_html += self.generate_frame_html(frame, center_lineno, is_collapsed) + is_collapsed = True + error = f"{traceback_obj.exc_type.__name__}: {traceback_obj}" return TEMPLATE.format(styles=STYLES, js=JS, error=error, exc_html=exc_html) From 184371cb1b25fde8b99de57a9739a9f4f27a6e8f Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Tue, 9 Jul 2019 14:47:47 +0100 Subject: [PATCH 2/3] Fix collapse button control initial states --- starlette/middleware/errors.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/starlette/middleware/errors.py b/starlette/middleware/errors.py index 547ff075..f06b705b 100644 --- a/starlette/middleware/errors.py +++ b/starlette/middleware/errors.py @@ -101,7 +101,7 @@ FRAME_TEMPLATE = """

File {frame_filename}, line {frame_lineno}, in {frame_name} - + {collapse_button}

{code_context}
@@ -199,7 +199,8 @@ class ServerErrorMiddleware: "frame_lineno": frame.lineno, "frame_name": frame.function, "code_context": code_context, - "collapsed": "collapsed" if is_collapsed else "" + "collapsed": "collapsed" if is_collapsed else "", + "collapse_button": "+" if is_collapsed else "‒", } return FRAME_TEMPLATE.format(**values) From a440047afb4cdcb7c2f8a7eec69fbafceaf9f110 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Tue, 9 Jul 2019 14:49:47 +0100 Subject: [PATCH 3/3] Linting --- starlette/middleware/errors.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/starlette/middleware/errors.py b/starlette/middleware/errors.py index f06b705b..9c9005c2 100644 --- a/starlette/middleware/errors.py +++ b/starlette/middleware/errors.py @@ -188,7 +188,9 @@ class ServerErrorMiddleware: return LINE.format(**values) return CENTER_LINE.format(**values) - def generate_frame_html(self, frame: inspect.FrameInfo, center_lineno: int, is_collapsed: bool) -> str: + def generate_frame_html( + self, frame: inspect.FrameInfo, center_lineno: int, is_collapsed: bool + ) -> str: code_context = "".join( self.format_line(context_position, line, frame.lineno, center_lineno) for context_position, line in enumerate(frame.code_context)