mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-07-22 11:59:33 +00:00
middleware: Add new middleware to handle common errors like DB busy
- During database error such as 'database is locked', show a special message asking users to try again instead of submitting a bug report. [sunil: Minor formatting, rename the template file name] Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org> [jvalleroy: Fix missing import] Signed-off-by: James Valleroy <jvalleroy@mailbox.org> Reviewed-by: James Valleroy <jvalleroy@mailbox.org> Reviewed-by: Sunil Mohan Adapa <sunil@medhas.org>
This commit is contained in:
parent
81cbd307f5
commit
557a3b2588
@ -10,8 +10,11 @@ from django.conf import settings
|
||||
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.shortcuts import render
|
||||
from django.template.response import SimpleTemplateResponse
|
||||
from django.utils.deprecation import MiddlewareMixin
|
||||
from django.utils.translation import gettext as _
|
||||
from stronghold.utils import is_view_func_public
|
||||
|
||||
from plinth import app as app_module
|
||||
@ -114,3 +117,19 @@ class FirstSetupMiddleware(MiddlewareMixin):
|
||||
'refresh_page_sec': 3
|
||||
}
|
||||
return render(request, 'first_setup.html', context)
|
||||
|
||||
|
||||
class CommonErrorMiddleware(MiddlewareMixin):
|
||||
"""Django middleware to handle common errors."""
|
||||
|
||||
@staticmethod
|
||||
def process_exception(request, exception):
|
||||
"""Show a custom error page when OperationalError is raised."""
|
||||
if isinstance(exception, OperationalError):
|
||||
message = _(
|
||||
'System is possibly under heavy load. Please retry later.')
|
||||
return SimpleTemplateResponse('error.html',
|
||||
context={'message': message},
|
||||
status=503)
|
||||
|
||||
return None
|
||||
|
||||
@ -135,6 +135,7 @@ MIDDLEWARE = (
|
||||
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
||||
'django.contrib.messages.middleware.MessageMiddleware',
|
||||
'django.middleware.clickjacking.XFrameOptionsMiddleware',
|
||||
'plinth.middleware.CommonErrorMiddleware',
|
||||
'stronghold.middleware.LoginRequiredMiddleware',
|
||||
'plinth.middleware.AdminRequiredMiddleware',
|
||||
'plinth.middleware.FirstSetupMiddleware',
|
||||
|
||||
16
plinth/templates/error.html
Normal file
16
plinth/templates/error.html
Normal file
@ -0,0 +1,16 @@
|
||||
{% extends 'base.html' %}
|
||||
{% comment %}
|
||||
# SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
{% endcomment %}
|
||||
|
||||
{% load i18n %}
|
||||
|
||||
{% block content %}
|
||||
|
||||
<h2>{% trans "Error" %}</h2>
|
||||
|
||||
<p>
|
||||
{{ message }}
|
||||
</p>
|
||||
|
||||
{% endblock %}
|
||||
Loading…
x
Reference in New Issue
Block a user