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 <sunil@medhas.org>
Reviewed-by: Veiko Aasa <veiko17@disroot.org>
This commit is contained in:
Sunil Mohan Adapa 2024-12-23 16:45:02 -08:00 committed by Veiko Aasa
parent 40e00423a8
commit 59a0a3b25f
No known key found for this signature in database
GPG Key ID: 478539CAE680674E
2 changed files with 18 additions and 10 deletions

View File

@ -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} <a href="#" class="dropdown-toggle" '
'data-bs-toggle="collapse" data-bs-target="#{collapse_id}" '
'aria-expanded="false" aria-controls="{collapse_id}">'
'Details</a><pre class="collapse" '
'id="{collapse_id}"><code>{html_message}</code></pre>',
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} <a href="#" class="dropdown-toggle" '
'data-bs-toggle="collapse" data-bs-target="#{collapse_id}" '
'aria-expanded="false" aria-controls="{collapse_id}">'
'Details</a><pre class="collapse" '
'id="{collapse_id}"><code>{html_message}</code></pre>',
message=message, html_message=html_message, collapse_id=collapse_id)
messages.error(request, formatted_message)
def _get_redirect_url_from_param(request):

View File

@ -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 */