From 59a0a3b25ff6d19f1ceb326ffe03788ef6cccf03 Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Mon, 23 Dec 2024 16:45:02 -0800 Subject: [PATCH] views: Show exception details with the utility to show errors - Instead of showing traceback details only for action exceptions, show them for normal exceptions also. - Also adjust the gap between the error message and the preformatted text to make it appear better. Tests: - Alter code to raise an exception with the utility. See that details are as expected. - Raise an exception in a privileged action, ensure that the details shown as before. Signed-off-by: Sunil Mohan Adapa Reviewed-by: Veiko Aasa --- plinth/views.py | 24 ++++++++++++++---------- static/themes/default/css/main.css | 4 ++++ 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/plinth/views.py b/plinth/views.py index 856a8485e..c23f4cd65 100644 --- a/plinth/views.py +++ b/plinth/views.py @@ -6,6 +6,7 @@ Main FreedomBox views. import datetime import random import time +import traceback import urllib.parse from django.contrib import messages @@ -116,17 +117,20 @@ def messages_error(request, message, exception): If an exception can show HTML message, handle is separately. """ if hasattr(exception, 'get_html_message'): - collapse_id = 'error-details-' + str(random.randint(0, 10**9)) - message = format_html( - '{message} ' - 'Details
{html_message}
', - message=message, html_message=exception.get_html_message(), - collapse_id=collapse_id) + html_message = exception.get_html_message() + else: + exception_lines = traceback.format_exception(exception) + html_message = ''.join(exception_lines) - messages.error(request, message) + collapse_id = 'error-details-' + str(random.randint(0, 10**9)) + formatted_message = format_html( + '{message} ' + 'Details
{html_message}
', + message=message, html_message=html_message, collapse_id=collapse_id) + messages.error(request, formatted_message) def _get_redirect_url_from_param(request): diff --git a/static/themes/default/css/main.css b/static/themes/default/css/main.css index 1e9a8813f..123570c59 100644 --- a/static/themes/default/css/main.css +++ b/static/themes/default/css/main.css @@ -343,6 +343,10 @@ html { max-width: 100%; } +.alert pre { + margin-top: 1rem; +} + /* Tags */ .tag { --bs-btn-padding-y: 0.25rem; /* Make the badge shorter */