From 26d317bfd5aa935f3957acfad1886f079ee8d3ca Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Sat, 28 Dec 2024 14:06:19 -0800 Subject: [PATCH] middleware: Handle page not found errors specially - Show a different message for them. Test: - Try to visit page like /plinth/apps/sharing/foo/edit/ where a share named 'foo' does not exist. The common error handling middleware is triggered and an alert message 'Page not found' with exception trace back is shown. Signed-off-by: Sunil Mohan Adapa Reviewed-by: Veiko Aasa --- plinth/middleware.py | 13 ++++++++++--- plinth/tests/test_middleware.py | 14 +++++++++++++- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/plinth/middleware.py b/plinth/middleware.py index a88963a68..ba67c13cd 100644 --- a/plinth/middleware.py +++ b/plinth/middleware.py @@ -11,7 +11,7 @@ from django.contrib import messages from django.contrib.auth.decorators import login_required from django.core.exceptions import PermissionDenied from django.db.utils import OperationalError -from django.http import HttpResponseNotAllowed +from django.http import Http404, HttpResponseNotAllowed from django.shortcuts import redirect, render from django.template.response import SimpleTemplateResponse from django.utils.deprecation import MiddlewareMixin @@ -143,12 +143,19 @@ class CommonErrorMiddleware(MiddlewareMixin): # to infinite redirects. return None - if request.method == 'POST': + if isinstance(exception, Http404): + message = _('Page not found: {url}').format(url=request.path) + exception = None # Don't show exception details + elif request.method == 'POST': message = _('Error running operation.') else: message = _('Error loading page.') - views.messages_error(request, message, exception) + if exception: + views.messages_error(request, message, exception) + else: + messages.error(request, message) + redirect_url = CommonErrorMiddleware._get_redirect_url_on_error( request) return redirect(redirect_url) diff --git a/plinth/tests/test_middleware.py b/plinth/tests/test_middleware.py index b025996cd..1c57b74f5 100644 --- a/plinth/tests/test_middleware.py +++ b/plinth/tests/test_middleware.py @@ -9,7 +9,7 @@ import pytest from django.contrib.auth.models import AnonymousUser, Group, User from django.core.exceptions import PermissionDenied from django.db.utils import OperationalError -from django.http import (HttpResponse, HttpResponseNotAllowed, +from django.http import (Http404, HttpResponse, HttpResponseNotAllowed, HttpResponseRedirect) from django.test.client import RequestFactory from django.urls import resolve @@ -294,6 +294,18 @@ class TestCommonErrorMiddleware: assert response.template_name == 'error.html' assert 'message' in response.context_data + @staticmethod + @patch('django.contrib.messages.error') + def test_404_error_get(messages_error, middleware, web_request, test_menu): + """Test that 404 page not found errors are handled.""" + response = middleware.process_exception(web_request, Http404()) + assert isinstance(response, HttpResponseRedirect) + assert response.url == '/apps/' + messages_error.assert_called_once() + assert messages_error.call_args.args[0] == web_request + assert messages_error.call_args.args[1].startswith( + 'Page not found: /apps/testapp/') + @staticmethod @patch('django.contrib.messages.error') def test_other_error_get(messages_error, middleware, web_request,