Browse Source

Add a generic error page for all HTTP errors

Previously the site would give an unstyled web-server error on a lot of
errors (such as trying to visit the page for a username that doesn't
exist). This just adds a generic error page, but we'll want to add some
more specialized ones for certain errors.
merge-requests/60/head
Deimos 6 years ago
parent
commit
fa27d41f6a
  1. 7
      tildes/tildes/templates/error_page.jinja2
  2. 32
      tildes/tildes/views/exceptions.py

7
tildes/tildes/templates/error_403.jinja2 → tildes/tildes/templates/error_page.jinja2

@ -3,12 +3,13 @@
{% extends 'base_no_sidebar.jinja2' %} {% extends 'base_no_sidebar.jinja2' %}
{% block title %}Error 403 (Forbidden){% endblock %}
{% block title %}{{ error }}{% endblock %}
{% block content %} {% block content %}
<div class="empty"> <div class="empty">
<h2 class="empty-title">Error 403 (Forbidden)</h2>
<p class="empty-subtitle">You don't have access to this page.</p>
<h2 class="empty-title">{{ error }}</h2>
<p class="empty-subtitle">{{ description }}</p>
<div class="empty-action"><a href="/" class="btn btn-primary">Back to the home page</a></div> <div class="empty-action"><a href="/" class="btn btn-primary">Back to the home page</a></div>
</div> </div>
{% endblock %} {% endblock %}

32
tildes/tildes/views/exceptions.py

@ -3,21 +3,18 @@
"""Views used by Pyramid when an exception is raised.""" """Views used by Pyramid when an exception is raised."""
from pyramid.httpexceptions import HTTPNotFound
from pyramid.httpexceptions import HTTPException, HTTPForbidden, HTTPNotFound
from pyramid.request import Request from pyramid.request import Request
from pyramid.view import forbidden_view_config, exception_view_config
from pyramid.view import (
exception_view_config,
forbidden_view_config,
notfound_view_config,
)
from sqlalchemy import cast, desc, func, Text from sqlalchemy import cast, desc, func, Text
from tildes.models.group import Group from tildes.models.group import Group
@forbidden_view_config(xhr=False, renderer="error_403.jinja2")
def forbidden(request: Request) -> dict:
"""403 Forbidden page."""
request.response.status_int = 403
return {}
@exception_view_config( @exception_view_config(
HTTPNotFound, route_name="group", renderer="error_group_not_found.jinja2" HTTPNotFound, route_name="group", renderer="error_group_not_found.jinja2"
) )
@ -34,3 +31,20 @@ def group_not_found(request: Request) -> dict:
.all() .all()
) )
return {"group_suggestions": group_suggestions, "supplied_name": supplied_name} return {"group_suggestions": group_suggestions, "supplied_name": supplied_name}
@notfound_view_config(xhr=False, renderer="error_page.jinja2")
@forbidden_view_config(xhr=False, renderer="error_page.jinja2")
@exception_view_config(context=HTTPException, xhr=False, renderer="error_page.jinja2")
def generic_error_page(request: Request) -> dict:
"""Display a generic error page for all HTTP exceptions."""
request.response.status_int = request.exception.status_int
error = f"Error {request.exception.status_code} ({request.exception.title})"
if isinstance(request.exception, HTTPForbidden):
description = "You don't have access to this page"
else:
description = request.exception.explanation
return {"error": error, "description": description}
Loading…
Cancel
Save