Skip to content

Commit

Permalink
Merge pull request #272 from maykinmedia/feature/264-relay-oz-errors
Browse files Browse the repository at this point in the history
Relay open-zaak errors
  • Loading branch information
SilviaAmAm committed Dec 22, 2021
2 parents 3bbb1ff + 44f4d68 commit 2d2dfef
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ <h1 class="title destruction-create__title">
<form action="" method="post">
{% csrf_token %}
{% if form.non_field_errors %}
<div class="archive-form__errors error-message">
<span class="material-icons">error_outline</span>{{ form.non_field_errors }}
<div class="archive-form__errors">
{% for error in form.non_field_errors %}
<div class="error-message"><span class="material-icons">error_outline</span>{{ error }}</div>
{% endfor %}
</div>
{% endif %}
{{ form.url.as_hidden }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,23 @@ def test_submit_successful_form_redirects(self, m_update_zaak, m_fetch_zaak):
@patch("archiefvernietigingscomponent.destruction.views.record_manager.fetch_zaak")
@patch(
"archiefvernietigingscomponent.destruction.views.record_manager.update_zaak",
side_effect=ClientError,
side_effect=ClientError(
{
"type": "http://127.0.0.1:8123/ref/fouten/ValidationError/",
"code": "invalid",
"title": "Invalid input.",
"status": 400,
"detail": "",
"instance": "urn:uuid:c8d9bf6b-4469-4b93-8411-1ebb5e4f4a40",
"invalidParams": [
{
"name": "nonFieldErrors",
"code": "documents-not-archived",
"reason": "Some error message",
}
],
}
),
)
@override_settings(LANGUAGE_CODE="en")
def test_client_error_during_update(self, m_update_zaak, m_fetch_zaak):
Expand All @@ -138,6 +154,9 @@ def test_client_error_during_update(self, m_update_zaak, m_fetch_zaak):
self.assertContains(
response, "An error has occurred. The case could not be updated."
)
self.assertContains(
response, "Some error message",
)

@patch(
"archiefvernietigingscomponent.destruction.views.record_manager.fetch_zaak",
Expand Down Expand Up @@ -171,3 +190,32 @@ def test_empty_archiefactiedatum(self, m_update_zaak, m_fetch_zaak):

self.assertEqual(1, len(messages))
self.assertEqual(messages[0].tags, "success")

@patch("archiefvernietigingscomponent.destruction.views.record_manager.fetch_zaak")
@patch(
"archiefvernietigingscomponent.destruction.views.record_manager.update_zaak",
side_effect=ClientError({"some_unexpected_keys": "some_unexpected_values"}),
)
@override_settings(LANGUAGE_CODE="en")
def test_unexpected_client_error_format_does_not_cause_error(
self, m_update_zaak, m_fetch_zaak
):
user = UserFactory(role__can_start_destruction=True)
view_url = furl(reverse("destruction:update-zaak-archive-details"))
view_url.args.set("url", "http://openzaak.nl/some/zaak")

form = self.app.get(view_url.url, user=user).form

form["url"] = "http://openzaak.nl/some/valid/zaak/url"
form["archiefnominatie"] = Archiefnominatie.blijvend_bewaren
form["archiefactiedatum_day"] = "1"
form["archiefactiedatum_month"] = "1"
form["archiefactiedatum_year"] = "2030"
form["comment"] = "Some interesting comment"

response = form.submit()

self.assertEqual(200, response.status_code)
self.assertContains(
response, "An error has occurred. The case could not be updated."
)
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
from datetime import date

from django.contrib.messages.views import SuccessMessageMixin
Expand Down Expand Up @@ -43,6 +44,8 @@
from ..service import fetch_zaak
from ..tasks import update_zaak, update_zaken

logger = logging.getLogger(__name__)

# Record manager views


Expand Down Expand Up @@ -323,11 +326,29 @@ def form_valid(self, form):
updated_data,
audit_comment=form.cleaned_data["comment"],
)
except ClientError:
except ClientError as exc:
# The client raised a 4xx error
form.add_error(
field=None,
error=_("An error has occurred. The case could not be updated."),
)
self.parse_errors(exc, form)
return super().form_invalid(form)

return super().form_valid(form)

@staticmethod
def parse_errors(exc, form):
error = exc.args[0]

try:
for param in error["invalidParams"]:
field_name = param["name"]
if field_name == "nonFieldErrors":
field_name = None

form.add_error(
field=field_name, error=param["reason"],
)
except KeyError as err:
logger.error("Encountered missing key %s in ClientError: %s", err, exc)
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
padding: 0.5em;
display: flex;
align-items: center;
margin-bottom: 1em;

.material-icons {
padding-right: 0.2em;
Expand Down

0 comments on commit 2d2dfef

Please sign in to comment.