From 39d78af5247443792390f873e490f21779705600 Mon Sep 17 00:00:00 2001 From: "Brian S. Stephan" Date: Sun, 21 Feb 2021 19:35:52 -0600 Subject: [PATCH] add error handler pages for 400, 404, 500 --- incorporealcms/__init__.py | 5 ++++- incorporealcms/error_pages.py | 17 +++++++++++++++++ incorporealcms/templates/400.html | 14 ++++++++++++++ incorporealcms/templates/404.html | 19 +++++++++++++++++++ incorporealcms/templates/500.html | 14 ++++++++++++++ incorporealcms/templates/base.html | 4 ++++ tests/functional_tests.py | 13 +++++++++++-- tests/instance/pages/actually-a-png.md | Bin 0 -> 49940 bytes 8 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 incorporealcms/error_pages.py create mode 100644 incorporealcms/templates/400.html create mode 100644 incorporealcms/templates/404.html create mode 100644 incorporealcms/templates/500.html create mode 100644 tests/instance/pages/actually-a-png.md diff --git a/incorporealcms/__init__.py b/incorporealcms/__init__.py index 45ceb25..726eaa3 100644 --- a/incorporealcms/__init__.py +++ b/incorporealcms/__init__.py @@ -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 diff --git a/incorporealcms/error_pages.py b/incorporealcms/error_pages.py new file mode 100644 index 0000000..5034fab --- /dev/null +++ b/incorporealcms/error_pages.py @@ -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 diff --git a/incorporealcms/templates/400.html b/incorporealcms/templates/400.html new file mode 100644 index 0000000..5f629bf --- /dev/null +++ b/incorporealcms/templates/400.html @@ -0,0 +1,14 @@ +{% extends "base.html" %} + +{% block header %} +
+ {{ config.TITLE_SUFFIX }} +
+{% endblock %} + +{% block body %} +
+

BAD REQUEST

+

You're doing something you're not supposed to. Stop it?

+
+{% endblock %} diff --git a/incorporealcms/templates/404.html b/incorporealcms/templates/404.html new file mode 100644 index 0000000..bac3191 --- /dev/null +++ b/incorporealcms/templates/404.html @@ -0,0 +1,19 @@ +{% extends "base.html" %} + +{% block header %} +
+ {{ config.TITLE_SUFFIX }} +
+{% endblock %} + +{% block body %} +
+

NOT FOUND

+

Sorry, {{ request.path }} does not seem to exist, at least not anymore.

+

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 @ <this domain> 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.

+

Otherwise, I suggest you go to the index and navigate your way (hopefully) to what + you're looking for.

+
+{% endblock %} diff --git a/incorporealcms/templates/500.html b/incorporealcms/templates/500.html new file mode 100644 index 0000000..68341a3 --- /dev/null +++ b/incorporealcms/templates/500.html @@ -0,0 +1,14 @@ +{% extends "base.html" %} + +{% block header %} +
+ {{ config.TITLE_SUFFIX }} +
+{% endblock %} + +{% block body %} +
+

INTERNAL SERVER ERROR

+

Something bad happened! Please email me at bss @ <this domain> and tell me what happened.

+
+{% endblock %} diff --git a/incorporealcms/templates/base.html b/incorporealcms/templates/base.html index d2772ab..e15878f 100644 --- a/incorporealcms/templates/base.html +++ b/incorporealcms/templates/base.html @@ -12,6 +12,7 @@
+ {% block header %}
+ {% endblock %} + {% block body %}
{{ content }}
Last modified: {{ mtime }}
+ {% endblock %}
diff --git a/tests/functional_tests.py b/tests/functional_tests.py index bb6ef20..6353ac4 100644 --- a/tests/functional_tests.py +++ b/tests/functional_tests.py @@ -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'/ohuesthaoeusth 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): diff --git a/tests/instance/pages/actually-a-png.md b/tests/instance/pages/actually-a-png.md new file mode 100644 index 0000000000000000000000000000000000000000..c592d258e2b8393c4435b4a83c2ddbd0020c3931 GIT binary patch literal 49940 zcmeHQ2~ZSQ8txtj7(k&x6cvSrAmSPYb%`JxGIsF(_V$W7ZYIn#EnOM2QSKSpyzJ5Ol)1kHfL2r(aTAwW;;^shX;;?%_?p z_x|tu|Nr~n>rR!02X(dVZAlPB*N|Xo1VNZFZ6AplI^)u1b1p%Uu?qqNWeeuaAqbE3 zKc<%r6o1^a*#CUWR9~4}vhVx8vb~y;5s#xYdPK*4``KsTeUzVn{pzAm^Wx*aiKiSc zTDO=SFWnZgX{$!?V)!}zs7Lw$ebc=cd86XU?e#~dJXzbiIrC^`H5Fs$baQcWHz$YiqpJ$; zkE^u4p!kQS*2=~wo~g7tFi(^?X_t-eqZ7ljcWwB*`tbKjasPIBWSZ+y?%8M1&gkZc zBdgtQ|GG0f{m!6#^DMWx+4JXm*LnBx z%PsuQU+dp^H?xR1mp89Eqb@59&49(y;NLGJ2asjrY-xriNRe1V!2t{cSOTB` zPyi?Z6aWeU1%LuT0iXa-04M+y015yFfC4}Ppa4+t+M+;L`~5(%NZc}0{cwx+;!jA@ zRgb51y85ctI{!*p;(I)uSJq9JTQD~TJE1Z=iV3~;2b?r4-^~*L$}f=O^y95!vBK_T zFPyf#r$5cs9UX)fp!<>Je(~asxV!toLPC#P7>2_m6T%P!0UM%i6k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