Skip to content

Commit

Permalink
Add http error generator page
Browse files Browse the repository at this point in the history
.. very quick and dirty, currently does not use specific error-templates
(for 404 and 500, say). Also attempts to log the error, or as an
exception in case of 500.
  • Loading branch information
hmpf authored Apr 12, 2021
1 parent 28db025 commit 25bbaf5
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/argus/site/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

from argus.auth.views import ObtainNewAuthToken
from argus.dataporten import views as dataporten_views
from argus.site.views import error


psa_urls = [
Expand All @@ -30,6 +31,7 @@
]

urlpatterns = [
# path(".error/", error), # Only needed when testing error pages and error behavior
path("admin/", admin.site.urls),
path("oidc/", include(psa_urls)),
path("api/schema/", SpectacularAPIView.as_view(api_version="v1"), name="schema-v1-old"),
Expand Down
61 changes: 61 additions & 0 deletions src/argus/site/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,66 @@
import logging

from django.http import (
HttpResponseBadRequest,
HttpResponseForbidden,
HttpResponseGone,
HttpResponseNotFound,
HttpResponseServerError,
)
from django.shortcuts import render


ERROR_TEMPLATE = """<html>
<head><title>{code} {reason}</title></head>
<body><h1>{code} {reason}</h1></body>
</html>
"""

LOG = logging.getLogger(__name__)


def index(request):
return render(request, "base.html")


def error(request):
def render_error_page(code, reason) -> bytes:
return ERROR_TEMPLATE.format(code=code, reason=reason).encode("utf-8")

ERROR_MAP = {
400: HttpResponseBadRequest,
403: HttpResponseForbidden,
404: HttpResponseNotFound,
410: HttpResponseGone,
500: HttpResponseServerError,
}
pp_errors = ", ".join([str(code) for code in sorted(ERROR_MAP.keys())])
status_code = request.GET.get("status-code", None)
if status_code is None:
errormsg = f'Status code "{status_code}" not in {pp_errors}'
content = render_error_page(400, errormsg)
LOG.error(f"{status_code} {errormsg}")
return HttpResponseBadRequest(content=content, reason=errormsg)
try:
status_code = int(status_code)
except ValueError:
errormsg = f'Status code "{status_code}" is not an integer'
content = render_error_page(400, errormsg)
LOG.error(f"{status_code} {errormsg}")
return HttpResponseBadRequest(content=content, reason=errormsg)
if status_code not in ERROR_MAP.keys():
errormsg = f'Status code "{status_code}" not in {pp_errors}'
content = render_error_page(400, errormsg)
LOG.error(f"{status_code} {errormsg}")
return HttpResponseBadRequest(content=content, reason=errormsg)

errormsg = f"{status_code} Generated error"
if status_code == 500:
try:
assert False, status_code
except AssertionError:
LOG.exception(errormsg)
else:
LOG.error(errormsg)
content = render_error_page(status_code, "Generated error page")
return ERROR_MAP[status_code](content=content)

0 comments on commit 25bbaf5

Please sign in to comment.