From a7eb8e930ca711a7a21131dbb3311e32741da494 Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Mon, 11 Sep 2023 11:38:45 +0530 Subject: [PATCH] Fix debug screen. --- main.py | 35 ++++++----------------------------- templates/show_error.html | 13 +++---------- 2 files changed, 9 insertions(+), 39 deletions(-) diff --git a/main.py b/main.py index a997b28..3e4bafd 100755 --- a/main.py +++ b/main.py @@ -1,6 +1,7 @@ #!/usr/bin/python3 """Check prices of ferries to France.""" +import collections import inspect import json import os @@ -8,7 +9,6 @@ import os.path import re import sys import traceback -import collections from datetime import date, datetime, timedelta from typing import Any @@ -23,11 +23,6 @@ import ferry from ferry.api import get_accommodations, get_prices from ferry.read_config import ferry_config, vehicle_from_config -# import werkzeug.exceptions -# from werkzeug.debug.tbtools import DebugTraceback -# from werkzeug.debug.tbtools import get_current_traceback - - app = flask.Flask(__name__) app.debug = True @@ -87,24 +82,23 @@ def get_duration(depart: str, arrive: str, time_delta: int) -> str: @app.errorhandler(werkzeug.exceptions.InternalServerError) -def exception_handler(e: Exception) -> tuple[str, int]: - """Show error page.""" +def exception_handler(e: werkzeug.exceptions.InternalServerError) -> tuple[str, int]: + """Handle exception.""" exec_type, exc_value, current_traceback = sys.exc_info() assert exc_value tb = DebugTraceback(exc_value) summary = tb.render_traceback_html(include_title=False) - exc_lines = list(tb._te.format_exception_only()) + exc_lines = "".join(tb._te.format_exception_only()) - frames = [f for f, lineno in traceback.walk_tb(current_traceback)] - last_frame = frames[-1] + last_frame = list(traceback.walk_tb(current_traceback))[-1][0] last_frame_args = inspect.getargs(last_frame.f_code) return ( flask.render_template( "show_error.html", plaintext=tb.render_traceback_text(), - exception="".join(exc_lines), + exception=exc_lines, exception_type=tb._te.exc_type.__name__, summary=summary, last_frame=last_frame, @@ -114,23 +108,6 @@ def exception_handler(e: Exception) -> tuple[str, int]: ) -# @app.errorhandler(werkzeug.exceptions.InternalServerError) -# def exception_handler(e): -# # tb = get_current_traceback() -# tb = DebugTraceback() -# last_frame = next(frame for frame in reversed(tb.frames) if not frame.is_library) -# last_frame_args = inspect.getargs(last_frame.code) -# return ( -# flask.render_template( -# "show_error.html", -# tb=tb, -# last_frame=last_frame, -# last_frame_args=last_frame_args, -# ), -# 500, -# ) - - def parse_date(d: str) -> date: """Parse a date from a string in ISO format.""" return datetime.strptime(d, "%Y-%m-%d").date() diff --git a/templates/show_error.html b/templates/show_error.html index 5d0cf27..dd79c3c 100644 --- a/templates/show_error.html +++ b/templates/show_error.html @@ -12,19 +12,12 @@
{{ exception }}
-{% set body %} -URL: {{ request.url }} - -{{ tb.plaintext | safe }} -{% endset %} -

Traceback (most recent call last)

{{ summary | safe }} -{# -

Error in function "{{ last_frame.function_name }}": {{ last_frame_args | pprint }}

-
{{ last_frame.locals | pprint }}
-#} +

Error in function "{{ last_frame.f_code.co_name }}": {{ last_frame_args | pprint }}

+
{{ last_frame.f_locals | pprint }}
+ {% endblock %}