Skip to content

Commit

Permalink
Convert uncaught exceptions to messages.error
Browse files Browse the repository at this point in the history
  • Loading branch information
hmpf committed Jan 24, 2025
1 parent a18cfcc commit a434cd5
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/argus/htmx/middleware.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import logging

from django.conf import settings
from django.contrib.auth.views import redirect_to_login
from django.http import HttpResponse
Expand All @@ -6,8 +8,11 @@
from django.utils.encoding import force_str
from django_htmx.http import HttpResponseClientRedirect
from django.contrib import messages

from .request import HtmxHttpRequest

LOG = logging.getLogger(__name__)


class LoginRequiredMiddleware:
def __init__(self, get_response):
Expand Down Expand Up @@ -61,6 +66,12 @@ class HtmxMessageMiddleware(MiddlewareMixin):

TEMPLATE = "messages/_notification_messages_htmx_append.html"

def process_exception(self, request, exception):
error_msg = f"500 Internal Server Error: {exception}"
messages.error(request, error_msg)
LOG.error(error_msg)
return None

def process_response(self, request: HtmxHttpRequest, response: HttpResponse) -> HttpResponse:
if not request.htmx:
return response
Expand All @@ -84,7 +95,11 @@ def process_response(self, request: HtmxHttpRequest, response: HttpResponse) ->
has_error_message = any("error" in message.tags for message in storage)
storage.used = False
if not has_error_message:
messages.error(request, "An error occured while processing your request, please try again")
error_msg = f"{response.status_code} {response.reason_phrase}"
messages.error(
request, f"An error occured while processing your request, please try again: {error_msg}"
)
LOG.error("Unhandled exception: %s", error_msg)
# HTMX doesn't swap content for response codes >=400. However, we do want to show
# the new messages, so we need to rewrite the response to 200, and make sure it only
# swaps the oob notification content
Expand Down

0 comments on commit a434cd5

Please sign in to comment.