Skip to content

Commit 1727de5

Browse files
committed
Clean up errors on each request
1 parent e8823ae commit 1727de5

File tree

3 files changed

+50
-17
lines changed

3 files changed

+50
-17
lines changed

lib/importer/index.js

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

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

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

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

@@ -169,8 +179,13 @@ exports.Initialise = (config, router, prototypeKit) => {
169179
// Redirects the current request to the 'next' URL after decoding the
170180
// encoded URI.
171181
//--------------------------------------------------------------------
172-
const redirectOnwards = (request, response) => {
173-
response.redirect(decodeURIComponent(request.query.next));
182+
const redirectOnwards = (request, response, failed=false) => {
183+
console.log(request.query)
184+
if (failed && request.query.error) {
185+
response.redirect(decodeURIComponent(request.query.error));
186+
} else {
187+
response.redirect(decodeURIComponent(request.query.next));
188+
}
174189
}
175190

176191
//--------------------------------------------------------------------
@@ -181,7 +196,6 @@ exports.Initialise = (config, router, prototypeKit) => {
181196
// time we run through the process.
182197
//--------------------------------------------------------------------
183198
router.all(IMPORTER_ROUTE_MAP.get("importerStartPath"),(request, response) => {
184-
cleanRequest(request);
185199
redirectOnwards(request, response);
186200
});
187201

@@ -192,8 +206,6 @@ exports.Initialise = (config, router, prototypeKit) => {
192206
const uploader = getUploader(config);
193207

194208
router.post(IMPORTER_ROUTE_MAP.get("importerUploadPath"), uploader.single("file"), (request, response) => {
195-
cleanRequest(request);
196-
197209
let createResponse = session_lib.CreateSession(config, request);
198210

199211
if (createResponse.error) {
@@ -320,6 +332,15 @@ exports.Initialise = (config, router, prototypeKit) => {
320332
}
321333

322334
session.mapping = request.body;
335+
if (Object.values(session.mapping).every((v) => v=='') ) {
336+
request.session.data[IMPORTER_ERROR_KEY] = "No columns were mapped to the expected fields"
337+
if (!request.query.error) {
338+
response.redirect(request.get('Referrer'))
339+
} else {
340+
redirectOnwards(request, response, failed=true);
341+
}
342+
return;
343+
}
323344

324345
// Ensure the session is persisted. Currently in session, eventually another way
325346
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)