Skip to content

Commit

Permalink
Render correct Http404 error response (#898)
Browse files Browse the repository at this point in the history
add check for Http404 exception and test case
  • Loading branch information
mitchdawson1982 authored Sep 30, 2024
1 parent 932e424 commit b9fe912
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 29 deletions.
9 changes: 9 additions & 0 deletions core/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from data_platform_catalogue.client.exceptions import ConnectivityError
from django.conf import settings
from django.core.exceptions import BadRequest
from django.http import Http404
from django.shortcuts import render
from django.utils.translation import gettext as _

Expand Down Expand Up @@ -34,6 +35,14 @@ def process_exception(self, request, exception):
context={"h1_value": _("Bad request")},
status=400,
)
elif isinstance(exception, Http404):
return render(
request,
"404.html",
context={"h1_value": _("Asset does not exist")},
status=404,
)

elif isinstance(exception, Exception):
return render(
request,
Expand Down
57 changes: 28 additions & 29 deletions locale/en/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@ msgid ""
msgstr ""
"Project-Id-Version: Find MoJ data\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-09-26 14:40+0100\n"
"POT-Creation-Date: 2024-09-30 11:15+0100\n"
"Language: en\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"

#: core/middleware.py:27
#: core/middleware.py:29
msgid "Catalogue service unavailable"
msgstr "Catalogue service unavailable"

#: core/middleware.py:34
#: core/middleware.py:36 core/middleware.py:43
msgid "Bad request"
msgstr "Bad request"

#: core/middleware.py:41
#: core/middleware.py:51
msgid "There is a problem with this service"
msgstr "There is a problem with this service"

Expand Down Expand Up @@ -126,7 +126,7 @@ msgstr "Title"
msgid "Name"
msgstr "Name"

#: home/service/search.py:240 templates/details_base.html:41
#: home/service/search.py:240 templates/details_base.html:47
#: templates/details_dashboard.html:14 templates/details_database.html:14
#: templates/details_table.html:14
msgid "Description"
Expand Down Expand Up @@ -172,7 +172,7 @@ msgstr ""
"The DataHub catalogue is currently unavailable. Please try again in a few "
"minutes."

#: templates/base/base.html:13
#: templates/base/base.html:27
msgid "Skip to main content"
msgstr "Skip to main content"

Expand Down Expand Up @@ -232,7 +232,7 @@ msgstr "Feedback"
msgid "Sign out"
msgstr "Sign out"

#: templates/base/navigation.html:69 templates/details_base.html:13
#: templates/base/navigation.html:69 templates/details_base.html:19
msgid "Search"
msgstr "Search"

Expand All @@ -244,23 +244,23 @@ msgstr "User guide"
msgid "Contact us"
msgstr "Contact us"

#: templates/details_base.html:47
#: templates/details_base.html:53
msgid "No description available."
msgstr "No description available."

#: templates/details_base.html:54
#: templates/details_base.html:60
msgid "First created:"
msgstr "First created:"

#: templates/details_base.html:60
#: templates/details_base.html:66
msgid "Last updated:"
msgstr "Last updated:"

#: templates/details_base.html:71 templates/partial/search_result.html:39
#: templates/details_base.html:77 templates/partial/search_result.html:39
msgid "Domain:"
msgstr "Subject area:"

#: templates/details_base.html:72 templates/partial/contact_info.html:15
#: templates/details_base.html:78 templates/partial/contact_info.html:15
#: templates/partial/contact_info.html:30
#: templates/partial/search_result.html:40
msgid "Not provided"
Expand All @@ -275,19 +275,19 @@ msgstr "Access this data"
msgid "%(display_name)s on Justice Data (opens in new tab)"
msgstr "%(display_name)s on Justice Data (opens in new tab)"

#: templates/details_dashboard.html:10 templates/details_dashboard.html:35
#: templates/details_dashboard.html:10 templates/details_dashboard.html:31
msgid "Dashboard content"
msgstr "Dashboard content"

#: templates/details_dashboard.html:13
msgid "Chart name"
msgstr "Chart name"

#: templates/details_dashboard.html:36
#: templates/details_dashboard.html:32
msgid "This dashboard is missing chart information."
msgstr "This dashboard is missing table information."

#: templates/details_dashboard.html:47
#: templates/details_dashboard.html:43
msgid "Justice Data (opens in new tab)"
msgstr "Justice Data (opens in new tab)"

Expand Down Expand Up @@ -354,35 +354,31 @@ msgstr ""
"label=\"Learn more about Find MoJ data\" href=\"https://user-guide.find-moj-"
"data.service.justice.gov.uk/\">Learn more</a>"

#: templates/home.html:18
msgid "Search metadata catalogue"
msgstr "Search for data"

#: templates/home.html:51
#: templates/home.html:47
msgid "Browse by domain"
msgstr "Browse by subject area"

#: templates/home.html:59
#: templates/home.html:55
msgid "Help us grow"
msgstr "Help us grow"

#: templates/home.html:60
#: templates/home.html:56
msgid ""
"Find MoJ data is a new service with a growing catalogue of data. You can "
"help us improve the service by:"
msgstr ""
"Find MoJ data is a new service with a growing catalogue of data. You can "
"help us improve the service by:"

#: templates/home.html:62
#: templates/home.html:58
msgid "adding a new data source"
msgstr "adding a new data source"

#: templates/home.html:63
#: templates/home.html:59
msgid "telling us about data you would like to see"
msgstr "telling us about data you would like to see"

#: templates/home.html:64
#: templates/home.html:60
msgid "giving us feedback"
msgstr "giving us feedback"

Expand Down Expand Up @@ -532,15 +528,15 @@ msgstr "Remove this filter"
msgid "Sort results"
msgstr "Sort results"

#: templates/search.html:15 templates/search.html:21
#: templates/search.html:15
msgid "Search MOJ data"
msgstr "Search MoJ data"

#: templates/search.html:29
#: templates/search.html:28
msgid "Search query tips"
msgstr "Search tips"

#: templates/search.html:34
#: templates/search.html:33
#, python-format
msgid ""
"<ul class=\"govuk-list govuk-list--bullet\"> <li><p>Use multiple words to "
Expand Down Expand Up @@ -570,7 +566,7 @@ msgstr ""
"li> </ul> <p><a href=\"%(more_examples_href)s\">More "
"advanced search options</a></p>"

#: templates/search.html:51
#: templates/search.html:50
msgid "Some results were malformed and are not shown"
msgstr "Some results were malformed and are not shown"

Expand All @@ -590,5 +586,8 @@ msgstr "first name"
msgid "last name"
msgstr "last name"

#~ msgid "Search metadata catalogue"
#~ msgstr "Search for data"

#~ msgid "Chart details"
#~ msgstr "Chart details"
13 changes: 13 additions & 0 deletions tests/core/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from data_platform_catalogue.client.exceptions import ConnectivityError
from django.core.exceptions import BadRequest
from django.http import Http404

from core.middleware import CustomErrorMiddleware

Expand Down Expand Up @@ -30,6 +31,18 @@ def test_middleware_renders_bad_request_response():
assert response.status_code == 400


def test_middleware_renders_http404_response():
get_response = MagicMock()
request = MagicMock()
middleware = CustomErrorMiddleware(get_response)
error = Http404()
response = middleware.process_exception(request, error)

assert response
assert b"Asset does not exist" in response.content
assert response.status_code == 404


def test_middleware_renders_unhandled_exception_response():
get_response = MagicMock()
request = MagicMock()
Expand Down

0 comments on commit b9fe912

Please sign in to comment.