Skip to content

Commit

Permalink
Refactor error summary handling and add partial templates for validat…
Browse files Browse the repository at this point in the history
…ion messages (#847)

* Refactor error summary handling and add partial templates for validation messages

- Introduced dynamic `errorKey` logic with fallback to a safe default.
- Added reusable partial `list-validation-messages.njk` to streamline validation message rendering.
- Created specific partial templates for error scenarios:
  - `error-summary-missing-prison-api-data.njk`
  - `error-summary-unsupported-offence.njk`
  - `error-summary-unsupported-sentence.njk`
  - `error-summary-unsupported-calculation.njk`
  - `error-summary-suspended-offence.njk`
  - `error-summary-save-dates.njk`
  - `error-summary-validation.njk`
  - `error-summary-default.njk`
- Updated `customErrorBanner.njk` to use dynamic titles and include error-specific partials.
- Simplified error summary structure for maintainability and extensibility.

Warning: I'm not sure there is full test coverage for this

* remove debug messages

---------

Co-authored-by: Joel Stobart <>
  • Loading branch information
joelstobart-moj authored Nov 15, 2024
1 parent 4e2510a commit b3397e4
Show file tree
Hide file tree
Showing 18 changed files with 88 additions and 56 deletions.
80 changes: 24 additions & 56 deletions server/views/pages/partials/customErrorBanner.njk
Original file line number Diff line number Diff line change
@@ -1,64 +1,32 @@
{% if validationErrors and validationErrors.messages.length > 0 %}
<div class="govuk-error-summary check-information-errors {% if validationErrors.messageType === 'MISSING_PRISON_API_DATA' %}information-summary{% endif %}" aria-labelledby="error-summary-title" role="alert" tabindex="-1" data-module="govuk-error-summary">

{% set validErrorKeys = [
"missing-prison-api-data",
"unsupported-offence",
"suspended-offence",
"unsupported-sentence",
"unsupported-calculation",
"validation",
"save-dates"
] %}

{% set rawErrorKey = validationErrors.messageType | lower | replace('_', '-') %}

{% if rawErrorKey in validErrorKeys %}
{% set errorKey = rawErrorKey %}
{% else %}
{% set errorKey = 'default' %}
{% endif %}

<div class="govuk-error-summary check-information-errors {% if errorKey === 'missing-prison-api-data' %}information-summary{% endif %}"
aria-labelledby="error-summary-title" role="alert" tabindex="-1" data-module="govuk-error-summary">
<h2 class="govuk-error-summary__title" id="error-summary-title">
{% if validationErrors.messageType === 'MISSING_PRISON_API_DATA' %}
The calculation breakdown cannot be shown on this page.
{% elif validationErrors.messageType === 'UNSUPPORTED_OFFENCE' %}
One or more offence codes needs updating in NOMIS.
{% elif validationErrors.messageType === 'SUSPENDED_OFFENCE' %}
‘Original offence suspended sentence activated’ is an invalid offence.
{% else %}
There is a problem
{% endif %}
{% include "./errorBanner/error-title-" + errorKey + ".njk" %}
</h2>
<div class="govuk-error-summary__body">
<ul class="govuk-list govuk-error-summary__list">
{% if validationErrors.messageType === 'UNSUPPORTED_SENTENCE' %}
<li>
<p class="govuk-body">One or more of the sentence types in this calculation is not currently supported in this service:</p>
</li>
{% elif validationErrors.messageType === 'UNSUPPORTED_CALCULATION' %}
<li>
<p class="govuk-body">This service does not yet support a calculation scenario{% if validationErrors.messages.length > 1 %}s{% endif %} when:</p>
</li>
{% endif %}
{% for error in validationErrors.messages %}
<li>
<p class="govuk-body">
{% if error.href %}
<a href="{{ error.href }}">{{ error.html | safe if error.html else error.text }}</a>
{% elif error.id %}
<a href="#{{ error.id }}">{{ error.html | safe if error.html else error.text }}</a>
{% else %}
{{ error.html | safe if error.html else error.text
| replace('\n', '<br>')
| replace ('reload this page', '<a href="">reload this page</a>') | safe }}
{% endif %}
</p>
</li>
{% endfor %}

{% if validationErrors.messageType === 'UNSUPPORTED_SENTENCE' %}
<li>
<p class="govuk-body">If these sentences are correct, you will need to complete this calculation manually in NOMIS.</p>
<p class="govuk-body govuk-!-margin-bottom-0">
<a href="{{'/supported-sentences/' + prisonerDetail.offenderNo}}">Check supported sentence types</a>
</p>
</li>
{% elif validationErrors.messageType === 'UNSUPPORTED_CALCULATION' %}
<li>
<p class="govuk-body govuk-!-margin-bottom-0">Calculate the release dates manually until {% if validationErrors.messages.length === 1 %}this scenario is{% else %}these scenarios are{% endif %} supported by this service.</p>
</li>
{% elif validationErrors.messageType === 'VALIDATION' %}
<li>
<p class="govuk-body govuk-!-margin-bottom-0">Update these details in NOMIS and then <a href="">reload this page</a>.</p>
</li>
{% elif validationErrors.messageType === 'SAVE_DATES' %}
<li>
<p class="govuk-body govuk-!-margin-bottom-0"> Review the calculation and try again.</p>
</li>
{% endif %}
{% include "./errorBanner/error-summary-" + errorKey + ".njk" %}
</ul>
</div>
</div>
{% endif %}
{% endif %}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% include "./list-validation-messages.njk" %}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% include "./list-validation-messages.njk" %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% include "./list-validation-messages.njk" %}

<li>
<p class="govuk-body govuk-!-margin-bottom-0">Review the calculation and try again.</p>
</li>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<li>
<p class="govuk-body">‘Original offence suspended sentence activated’ is an invalid offence.</p>
</li>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<li>
<p class="govuk-body">This service does not yet support a calculation scenario when:</p>
</li>

{% include "./list-validation-messages.njk" %}

<li>
<p class="govuk-body govuk-!-margin-bottom-0">Calculate the release dates manually until these scenarios are
supported by this service.</p>
</li>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<li>
<p class="govuk-body">One or more offence codes need updating in NOMIS.</p>
</li>

{% include "./list-validation-messages.njk" %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<li>
<p class="govuk-body">One or more of the sentence types in this calculation is not currently supported in this
service:</p>
</li>

{% include "./list-validation-messages.njk" %}

<li>
<p class="govuk-body">If these sentences are correct, you will need to complete this calculation manually in
NOMIS.</p>
<p class="govuk-body govuk-!-margin-bottom-0">
<a href="{{ '/supported-sentences/' + prisonerDetail.offenderNo }}">Check supported sentence types</a>
</p>
</li>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{% include "./list-validation-messages.njk" %}

<li>
<p class="govuk-body govuk-!-margin-bottom-0">Update these details in NOMIS and then <a href="">reload this page</a>.
</p>
</li>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
There is a problem
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The calculation breakdown cannot be shown on this page.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Review the calculation and try again.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
&lsquo;Original offence suspended sentence activated&rsquo; is an invalid offence.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This service does not yet support a calculation scenario.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
One or more offence codes need updating in NOMIS.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
One or more of the sentence types in this calculation is not currently supported.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
There is a problem
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% for error in validationErrors.messages %}
<li>
<p class="govuk-body">
{% if error.href %}
<a href="{{ error.href }}">{{ error.html | safe if error.html else error.text }}</a>
{% else %}
{{ error.html | safe if error.html else error.text | replace('\n', '<br>') | safe }}
{% endif %}
</p>
</li>
{% endfor %}

0 comments on commit b3397e4

Please sign in to comment.