diff --git a/tildes/tildes/templates/error_403.jinja2 b/tildes/tildes/templates/error_page.jinja2 similarity index 64% rename from tildes/tildes/templates/error_403.jinja2 rename to tildes/tildes/templates/error_page.jinja2 index 749f047..5bb8afa 100644 --- a/tildes/tildes/templates/error_403.jinja2 +++ b/tildes/tildes/templates/error_page.jinja2 @@ -3,12 +3,13 @@ {% extends 'base_no_sidebar.jinja2' %} -{% block title %}Error 403 (Forbidden){% endblock %} +{% block title %}{{ error }}{% endblock %} {% block content %}
-

Error 403 (Forbidden)

-

You don't have access to this page.

+

{{ error }}

+

{{ description }}

+
Back to the home page
{% endblock %} diff --git a/tildes/tildes/views/exceptions.py b/tildes/tildes/views/exceptions.py index f7a42d4..40ac4b8 100644 --- a/tildes/tildes/views/exceptions.py +++ b/tildes/tildes/views/exceptions.py @@ -3,21 +3,18 @@ """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.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 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( HTTPNotFound, route_name="group", renderer="error_group_not_found.jinja2" ) @@ -34,3 +31,20 @@ def group_not_found(request: Request) -> dict: .all() ) 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}