Skip to content

Commit

Permalink
feat: Add global exception handling and custom 503 error page
Browse files Browse the repository at this point in the history
  • Loading branch information
albertoleoncio committed Dec 28, 2024
1 parent 70f990f commit 9dbb4ae
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
23 changes: 23 additions & 0 deletions templates/503.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!DOCTYPE html>
<html lang="pt-br">
<head>
<title>Toolforge Database Error - WikiScore</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="/static/w3.css">
<link href="https://tools-static.wmflabs.org/fontcdn/css?family=Roboto" rel="stylesheet" type="text/css">
</head>
<body style="font-family: 'Roboto', sans-serif;">

<!-- Header -->
<header class="w3-container w3-content w3-padding-32 w3-center">
<img alt="logo" src="/static/Logo_Preto_Tagline.svg" style="width: 300px;">
<h1 class="w3-jumbo w3-text-red">Toolforge Database Error</h1>
<p class="w3-xlarge">We encountered a database connection issue with Toolforge.</p>
<p class="w3-xlarge">This is a temporary issue with Toolforge's database infrastructure and not a problem with our service.</p>
<p class="w3-xlarge">Please try again in a few minutes. We apologize for the inconvenience.</p>
<button class="w3-button w3-black w3-padding w3-margin-top w3-round-xxlarge" onclick="location.href='/'">Return to homepage</button>
</header>

</body>
</html>
19 changes: 19 additions & 0 deletions wikiscore/middleware.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from django.shortcuts import render
import traceback

class GlobalExceptionMiddleware:
def __init__(self, get_response):
self.get_response = get_response

def __call__(self, request):
try:
response = self.get_response(request)
return response
except AttributeError as e:
tb = traceback.format_exc()
if "'NoneType' object has no attribute 'settimeout'" in str(e) and "pymysql" in tb:
# Return a 503 Service Unavailable response
response = render(request, '503.html', status=503)
else:
raise
return response
1 change: 1 addition & 0 deletions wikiscore/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'social_django.middleware.SocialAuthExceptionMiddleware',
'wikiscore.middleware.GlobalExceptionMiddleware',
]

ROOT_URLCONF = 'wikiscore.urls'
Expand Down

0 comments on commit 9dbb4ae

Please sign in to comment.