From 0a2412525fae7747ed0603e2e946cd197c421f30 Mon Sep 17 00:00:00 2001 From: Steven Loria Date: Sun, 8 Dec 2013 02:34:14 -0600 Subject: [PATCH] Add error handlers and templates for 401, 404, and 500 --- .../{{cookiecutter.repo_name}}/app.py | 12 +++++++- .../templates/401.html | 16 ++++++++++ .../templates/404.html | 29 +++++-------------- .../templates/500.html | 14 +++++++++ 4 files changed, 48 insertions(+), 23 deletions(-) create mode 100644 {{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/templates/401.html create mode 100644 {{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/templates/500.html diff --git a/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/app.py b/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/app.py index d45a183..daa7b88 100644 --- a/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/app.py +++ b/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/app.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- -from flask import Flask +'''The app module, containing the app factory function.''' +from flask import Flask, render_template from flask_debugtoolbar import DebugToolbarExtension from {{cookiecutter.repo_name}}.settings import ProdConfig @@ -19,6 +20,7 @@ def create_app(config_object=ProdConfig): app.config.from_object(config_object) register_extensions(app) register_blueprints(app) + register_errorhandlers(app) return app @@ -36,3 +38,11 @@ def register_blueprints(app): app.register_blueprint(public.views.blueprint) app.register_blueprint(user.views.blueprint) return None + + +def register_errorhandlers(app): + def render_error(error): + return render_template("{0}.html".format(error.code)), error.code + for errcode in [401, 404, 500]: + app.errorhandler(errcode)(render_error) + return None diff --git a/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/templates/401.html b/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/templates/401.html new file mode 100644 index 0000000..3f45f7f --- /dev/null +++ b/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/templates/401.html @@ -0,0 +1,16 @@ +{% raw %} +{% extends "_layouts/base.html" %} + +{% block page_title %}Unauthorized{% endblock %} + +{% block content %} +
+
+

401

+

You are not authorized to see this page. Please log in or + create a new account. +

+
+
+{% endblock %} +{% endraw %} diff --git a/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/templates/404.html b/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/templates/404.html index 212efec..4d49138 100644 --- a/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/templates/404.html +++ b/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/templates/404.html @@ -3,28 +3,13 @@ {% block page_title %}Page Not Found{% endblock %} -{% block css %} - -{% endblock %} - {% block content %} -

404

-

Sorry, that page doesn't exist.

-

Want to go home instead?

+
+
+

404

+

Sorry, that page doesn't exist.

+

Want to go home instead?

+
+
{% endblock %} {% endraw %} diff --git a/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/templates/500.html b/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/templates/500.html new file mode 100644 index 0000000..db0696d --- /dev/null +++ b/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/templates/500.html @@ -0,0 +1,14 @@ +{% raw %} +{% extends "_layouts/base.html" %} + +{% block page_title %}Server error{% endblock %} + +{% block content %} +
+
+

500

+

Sorry, something went wrong on our system. Don't panic, we are fixing it! Please try again later.

+
+
+{% endblock %} +{% endraw %}