Skip to content

Commit 4f3f54a

Browse files
authored
Merge pull request #465 from WikipediaLibrary/dhardy/T350820
Add custom 4xx/5xx error page templates
2 parents 773c285 + a545855 commit 4f3f54a

File tree

8 files changed

+92
-0
lines changed

8 files changed

+92
-0
lines changed

docker-compose.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ services:
8888
- type: bind
8989
source: ./static
9090
target: /app/static
91+
- type: bind
92+
source: ./extlinks/templates/500
93+
target: /app/500
9194
ports:
9295
- "80:80"
9396
depends_on:

extlinks/templates/400.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{% extends 'base.html' %}
2+
3+
{% block content %}
4+
<div class="body">
5+
<h1>400 Bad Request</h1>
6+
<p>Sorry; we don't know what to do with that.</p>
7+
</div>
8+
{% endblock %}

extlinks/templates/403.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{% extends 'base.html' %}
2+
3+
{% block content %}
4+
<div class="body">
5+
<h1>403 Forbidden</h1>
6+
<p>Sorry; you aren't allowed to do that.</p>
7+
</div>
8+
{% endblock %}

extlinks/templates/404.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{% extends 'base.html' %}
2+
3+
{% block content %}
4+
<div class="body">
5+
<h1>404 Not Found</h1>
6+
<p>Sorry; we can't find that.</p>
7+
</div>
8+
{% endblock %}

extlinks/templates/500/500.html

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1">
6+
7+
<title>Wikimedia External Links Tool</title>
8+
9+
<link rel="icon" type="image/png" href="/static/favicon.ico">
10+
<link rel="stylesheet" href="https://tools-static.wmflabs.org/cdnjs/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
11+
<link rel="stylesheet" href="/static/css/local.css" type="text/css">
12+
</head>
13+
14+
<body>
15+
<nav class="navbar navbar-expand-lg navbar-light shadow-sm">
16+
<a class="navbar-brand" href="/">Wikilink</a>
17+
<div class="navbar-nav">
18+
<a class="nav-item nav-link" href="/programs/">Programs</a>
19+
<a class="nav-item nav-link" href="/organisations/">Organisations</a>
20+
<a class="nav-item nav-link" href="/docs">What is this?</a>
21+
</div>
22+
</nav>
23+
24+
<div class="main-content">
25+
<div class="body">
26+
<h1>500 Internal Server Error</h1>
27+
<p>Sorry; something went wrong.</p>
28+
</div>
29+
</div>
30+
31+
<hr/>
32+
33+
<div class="footer">
34+
A <a href="https://meta.wikimedia.org/wiki/The_Wikipedia_Library">Wikipedia Library</a> project -
35+
<a href="https://github.com/WikipediaLibrary/externallinks">Github</a> -
36+
<a href="https://phabricator.wikimedia.org/project/board/4082/">Phabricator</a> -
37+
<a href="https://meta.wikimedia.org/wiki/Wikilink_tool">Meta</a>
38+
</div>
39+
40+
<script type="text/javascript" src="https://tools-static.wmflabs.org/cdnjs/ajax/libs/twitter-bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
41+
</body>
42+
</html>

extlinks/urls.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22
from django.contrib import admin
33
from django.urls import include, path
44
from django.conf import settings
5+
from django.views.generic import TemplateView
56

67
from extlinks.healthcheck.urls import urlpatterns as healthcheck_urls
78
from extlinks.programs.urls import urlpatterns as programs_urls
89
from extlinks.organisations.urls import urlpatterns as organisations_urls
910

1011
from .views import Homepage, Documentation
1112

13+
handler500 = "extlinks.views.custom_server_error"
14+
1215
urlpatterns = [
1316
path("admin/", admin.site.urls),
1417
path("", Homepage.as_view(), name="homepage"),
@@ -31,4 +34,8 @@
3134

3235
urlpatterns += [
3336
path("__debug__/", include(debug_toolbar.urls)),
37+
path("400/", TemplateView.as_view(template_name="400.html")),
38+
path("403/", TemplateView.as_view(template_name="403.html")),
39+
path("404/", TemplateView.as_view(template_name="404.html")),
40+
path("500/", TemplateView.as_view(template_name="500/500.html")),
3441
]

extlinks/views.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
from django.views.decorators.csrf import requires_csrf_token
12
from django.views.generic import TemplateView
3+
from django.views.defaults import server_error
24

35

46
class Homepage(TemplateView):
@@ -7,3 +9,7 @@ class Homepage(TemplateView):
79

810
class Documentation(TemplateView):
911
template_name = "documentation.html"
12+
13+
@requires_csrf_token
14+
def custom_server_error(request):
15+
return server_error(request, "500/500.html")

nginx.conf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ server {
8080
# checks for static file, if not found proxy to app
8181
try_files $uri @django;
8282
}
83+
8384
location @django {
8485
# Cache
8586
proxy_cache_valid 200 301 302 401 403 404 1d;
@@ -97,6 +98,7 @@ server {
9798
proxy_redirect off;
9899
proxy_pass http://django_server;
99100
}
101+
100102
location @django-admin-slow {
101103
# https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_send_timeout
102104
proxy_connect_timeout 120s;
@@ -121,4 +123,12 @@ server {
121123
proxy_redirect off;
122124
proxy_pass http://django_server;
123125
}
126+
127+
proxy_intercept_errors on;
128+
error_page 500 501 502 503 504 505 506 /500.html;
129+
130+
location = /500.html {
131+
root /app/500;
132+
internal;
133+
}
124134
}

0 commit comments

Comments
 (0)