add error handler pages for 400, 404, 500

This commit is contained in:
Brian S. Stephan 2021-02-21 19:35:52 -06:00
parent e570ee26b5
commit 39d78af524
8 changed files with 83 additions and 3 deletions

View File

@ -45,7 +45,10 @@ def create_app(instance_path=None, test_config=None):
return send_from_directory(os.path.join(app.instance_path, app.config['MEDIA_DIR']),
filename)
from . import pages
from . import error_pages, pages
app.register_blueprint(pages.bp)
app.register_error_handler(400, error_pages.bad_request)
app.register_error_handler(404, error_pages.page_not_found)
app.register_error_handler(500, error_pages.internal_server_error)
return app

View File

@ -0,0 +1,17 @@
"""Error page views for 400, 404, etc."""
from incorporealcms.lib import render
def bad_request(error):
"""Display 400 error messaging."""
return render('400.html'), 400
def internal_server_error(error):
"""Display 500 error messaging."""
return render('500.html'), 500
def page_not_found(error):
"""Display 404 error messaging."""
return render('404.html'), 404

View File

@ -0,0 +1,14 @@
{% extends "base.html" %}
{% block header %}
<div class="header">
<a href="/">{{ config.TITLE_SUFFIX }}</a>
</div>
{% endblock %}
{% block body %}
<div class="content">
<h1>BAD REQUEST</h1>
<p>You're doing something you're not supposed to. Stop it?</p>
</div>
{% endblock %}

View File

@ -0,0 +1,19 @@
{% extends "base.html" %}
{% block header %}
<div class="header">
<a href="/">{{ config.TITLE_SUFFIX }}</a>
</div>
{% endblock %}
{% block body %}
<div class="content">
<h1>NOT FOUND</h1>
<p>Sorry, <b><tt>{{ request.path }}</tt></b> does not seem to exist, at least not anymore.</p>
<p>It's possible you followed a dead link on this site, in which case I would appreciate it if you could email me via:
bss @ &lt;this domain&gt; and I can take a look. I make an effort to symlink old content to its new location,
so old links and URLs should, generally speaking, work.</p>
<p>Otherwise, I suggest you go <a href="/">to the index</a> and navigate your way (hopefully) to what
you're looking for.</p>
</div>
{% endblock %}

View File

@ -0,0 +1,14 @@
{% extends "base.html" %}
{% block header %}
<div class="header">
<a href="/">{{ config.TITLE_SUFFIX }}</a>
</div>
{% endblock %}
{% block body %}
<div class="content">
<h1>INTERNAL SERVER ERROR</h1>
<p>Something bad happened! Please email me at bss @ &lt;this domain&gt; and tell me what happened.</p>
</div>
{% endblock %}

View File

@ -12,6 +12,7 @@
<link rel="icon" href="{{ url_for('static', filename='img/favicon.png') }}">
<div class="site-wrap">
{% block header %}
<div class="header">
<div class="nav">
{% for nav in navs %}
@ -24,11 +25,14 @@
<a href="?style=light">[light]</a>
</div>
</div>
{% endblock %}
{% block body %}
<div class="content">
{{ content }}
</div>
<footer>
<i>Last modified: {{ mtime }}</i>
</footer>
{% endblock %}
</div>
</html>

View File

@ -10,15 +10,24 @@ def test_page_that_exists(client):
def test_page_that_doesnt_exist(client):
"""Test that the app returns 404 for nonsense requests."""
"""Test that the app returns 404 for nonsense requests and they use my error page."""
response = client.get('/ohuesthaoeusth')
assert response.status_code == 404
assert b'<b><tt>/ohuesthaoeusth</tt></b> does not seem to exist' in response.data
def test_files_outside_pages_do_not_get_served(client):
"""Test that page pathing doesn't break out of the instance/pages/ dir."""
"""Test that page pathing doesn't break out of the instance/pages/ dir, and the error uses my error page."""
response = client.get('/../unreachable')
assert response.status_code == 400
assert b'You\'re doing something you\'re not supposed to. Stop it?' in response.data
def test_internal_server_error_serves_error_page(client):
"""Test that various exceptions serve up the 500 page."""
response = client.get('/actually-a-png')
assert response.status_code == 500
assert b'INTERNAL SERVER ERROR' in response.data
def test_weird_paths_do_not_get_served(client):

Binary file not shown.

After

Width:  |  Height:  |  Size: 49 KiB