Skip to content

Commit

Permalink
Merge pull request #171 from pygame/404
Browse files Browse the repository at this point in the history
app: templates/error: Add error handler for 404 and 500
  • Loading branch information
illume authored Sep 23, 2023
2 parents 0f1d44b + 0754915 commit a008aec
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 78 deletions.
33 changes: 16 additions & 17 deletions pygameweb/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,29 +69,30 @@ def _url_for_security(endpoint, **values):
def inject_url_for_image():
return {'url_for_security': _url_for_security}

def add_error_handlers(app):
""" Add error handlers to the app, like for a 404 not found. """
def error_handler(error):
message = error.description
if ' ' in message:
message, sub = message.split(' ', 1)
else:
sub = ''
return render_template('error.html', code=error.code, message=message, sub=sub), error.code

def http_error_handler(error):
message = error.description
if ' ' in message:
message, sub = message.split(' ', 1)
else:
sub = ''
return render_template('error.html', code=error.code, message=message, sub=sub), error.code
@app.errorhandler(404)
def page_not_found(error):
return error_handler(error)


def error_handler(_):
message = 'Internal Server Error'
return render_template('error.html', code=500, message=message, sub=''), 500
@app.errorhandler(500)
def internal_server_error(error):
return error_handler(error)


def add_views_front(app):
""" Adds all the front end views to the app.
Kept separate from create_app so we can test individual views.
"""
# We catch HTTPException to handle all HTTP error codes
from werkzeug.exceptions import HTTPException

# add_user_blueprint does some monkey patching, so it needs to be first.
from pygameweb.user.views import add_user_blueprint
add_user_blueprint(app)
Expand All @@ -109,9 +110,7 @@ def add_views_front(app):
from pygameweb.builds.views import add_builds
from pygameweb.comment.views import add_comment

# app.errorhandler(HTTPException)(http_error_handler)
# app.errorhandler(Exception)(error_handler)

add_error_handlers(app)
add_wiki_blueprint(app)
add_project_blueprint(app)
add_thumb_blueprint(app)
Expand Down
84 changes: 23 additions & 61 deletions pygameweb/templates/error.html
Original file line number Diff line number Diff line change
@@ -1,61 +1,23 @@
<!DOCTYPE html>
<html>
<head>
<title>{{ code }} - pygame.org</title>

<meta name="viewport" content="width=device-width, initial-scale=1.0"/>

<style>
* {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
text-align: center;
color: #111;
}

h1 {
margin-top: 128px;
font-size: 10em;
margin-bottom: 16px;
}

h2 {
font-size: 1.9em;
font-weight: 400;

padding: 0 64px;
}

h3 {
font-weight: 400;
}

p {
font-size: 1.2em;
color: #444;
}

a {
color: #337ab7;
text-decoration: none;
}

img {
height: 100px;
}
</style>

</head>
<body{% block body_attribs %}{% endblock body_attribs %}>

<div id="container">
<img src="https://www.pygame.org/docs/pygame_logo.gif">
<h1>{{ code }}</h1>
<h2>{{ message }}</h2>
<h3>{{ sub }}</h3>
<p><a href="/">Home</a> - <a href="/docs">Docs</a> - <a
target="_blank" href="https://github.com/pygame/pygameweb/issues/new"
>Report This</a></p>
</div>

</body>
</html>
{% extends "base.html" %}

{% block title -%}
{% cache 60*5, 'error_page_title', code|string, message, sub -%}
Error, {{ code }}, {{ message }}
{% endcache -%}
{% endblock -%}

{% block content %}
{% cache 60*5, 'error_page', code|string, message, sub %}
<section class="error">
<h1>{{ code }}</h1>
<h2>{{ message }}</h2>
<h3>{{ sub }}</h3>

<p><a href="/">Home</a> - <a href="/docs">Docs</a> - <a
target="_blank" href="https://github.com/pygame/pygameweb/issues/new"
>Help Fix This</a></p>
</section>
{% endcache %}
{% endblock %}

{% block body_attribs %}{{ super() }}{% endblock body_attribs %}

0 comments on commit a008aec

Please sign in to comment.