Skip to content

Commit 616dda9

Browse files
committed
Clean up errors on each request
1 parent 9940226 commit 616dda9

3 files changed

Lines changed: 50 additions & 17 deletions

File tree

lib/importer/index.js

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,28 +29,38 @@ exports.Initialise = (config, router, prototypeKit) => {
2929
// one created by us.
3030
config.uploadPath ??= path.join(os.tmpdir(), 'reg-dyn-importer'); ;
3131

32+
3233
//--------------------------------------------------------------------
3334
// Removes any previous importer error from the session. When we set
3435
// an error we redirect to the referer and we expect that page to show
3536
// the error. Calling this in each POST endpoint ensures that we don't
3637
// remember errors after they have been shown,
3738
//--------------------------------------------------------------------
38-
const cleanRequest = (request) => {
39+
router.all("*", (request, res, next) => {
3940
delete request.session.data[IMPORTER_ERROR_KEY]
40-
}
41+
next();
42+
});
43+
4144

4245
//--------------------------------------------------------------------
4346
// Make the route functions available in the templates. These functions
4447
// allow users to find the path that they should use to submit data,
4548
// and also allows them to specify which url to redirect to after
46-
// the POST data has been processed.
49+
// the POST data has been processed. Where an extra error url is provided
50+
// it will be used if the POST request does not succeed (e.g. when
51+
// mapping the data this can be used to redirect to the review page
52+
// to view warnings)
4753
//--------------------------------------------------------------------
4854
for ([key, value] of IMPORTER_ROUTE_MAP) {
4955
const k = key;
5056
const v = value;
5157

52-
prototypeKit.views.addFunction(k, (next)=>{
53-
return `${v}?next=${encodeURIComponent(next)}`;
58+
prototypeKit.views.addFunction(k, (next, errorPage=null)=>{
59+
let url = `${v}?next=${encodeURIComponent(next)}`;
60+
if (errorPage) {
61+
url = url + `&error=${encodeURIComponent(errorPage)}`
62+
}
63+
return url
5464
}, {})
5565
}
5666

@@ -140,8 +150,13 @@ exports.Initialise = (config, router, prototypeKit) => {
140150
// Redirects the current request to the 'next' URL after decoding the
141151
// encoded URI.
142152
//--------------------------------------------------------------------
143-
const redirectOnwards = (request, response) => {
144-
response.redirect(decodeURIComponent(request.query.next));
153+
const redirectOnwards = (request, response, failed=false) => {
154+
console.log(request.query)
155+
if (failed && request.query.error) {
156+
response.redirect(decodeURIComponent(request.query.error));
157+
} else {
158+
response.redirect(decodeURIComponent(request.query.next));
159+
}
145160
}
146161

147162
//--------------------------------------------------------------------
@@ -152,7 +167,6 @@ exports.Initialise = (config, router, prototypeKit) => {
152167
// time we run through the process.
153168
//--------------------------------------------------------------------
154169
router.all(IMPORTER_ROUTE_MAP.get("importerStartPath"),(request, response) => {
155-
cleanRequest(request);
156170
redirectOnwards(request, response);
157171
});
158172

@@ -163,8 +177,6 @@ exports.Initialise = (config, router, prototypeKit) => {
163177
const uploader = getUploader(config);
164178

165179
router.post(IMPORTER_ROUTE_MAP.get("importerUploadPath"), uploader.single("file"), (request, response) => {
166-
cleanRequest(request);
167-
168180
let createResponse = session_lib.CreateSession(config, request);
169181

170182
if (createResponse.error) {
@@ -274,6 +286,15 @@ exports.Initialise = (config, router, prototypeKit) => {
274286
}
275287

276288
session.mapping = request.body;
289+
if (Object.values(session.mapping).every((v) => v=='') ) {
290+
request.session.data[IMPORTER_ERROR_KEY] = "No columns were mapped to the expected fields"
291+
if (!request.query.error) {
292+
response.redirect(request.get('Referrer'))
293+
} else {
294+
redirectOnwards(request, response, failed=true);
295+
}
296+
return;
297+
}
277298

278299
// Ensure the session is persisted. Currently in session, eventually another way
279300
request.session.data[IMPORTER_SESSION_KEY] = session;

lib/importer/nunjucks/importer/macros/field_mapper.njk

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,25 @@
1717
{% macro importerFieldMapper(data, caption='', columnTitle='Column', examplesTitle='Example values', fieldsTitle='Fields') %}
1818
{% set fields = data['importer.session']['fields'] %}
1919
{% set headings = importerGetHeaders(data) %}
20-
{% set error = headings.error %}
20+
{% set error = importerError(data) %}
2121

22-
{% if error %}
23-
<p id="mapping-error" class="govuk-error-message">
24-
<span class="govuk-visually-hidden">Error:</span> {{ error.text }}
25-
</p>
26-
{% endif %}
22+
23+
{% if error %}
24+
<div class="govuk-error-summary" data-module="govuk-error-summary">
25+
<div role="alert">
26+
<h2 class="govuk-error-summary__title">
27+
There was a problem submitting this data
28+
</h2>
29+
<div class="govuk-error-summary__body">
30+
<ul class="govuk-list govuk-error-summary__list">
31+
<li>
32+
{{ error.text }}
33+
</li>
34+
</ul>
35+
</div>
36+
</div>
37+
</div>
38+
{% endif %}
2739

2840

2941
<table class="govuk-table">

prototypes/basic/app/views/mapping.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ <h1 class="govuk-heading-l">Map columns</h1>
1414

1515
<h2 class="govuk-heading-m">{{sheet}}</h2>
1616

17-
<form action="{{ importerMapDataPath('/success') }}" method="post">
17+
<form action="{{ importerMapDataPath('/success', '/review') }}" method="post">
1818

1919
{{ importerFieldMapper(data) }}
2020

0 commit comments

Comments
 (0)