Skip to content

Commit ae2714b

Browse files
authored
Use middleware to ensure errors on the page are cleaned up on next request (#292)
We use the session to hold error information to be displayed on the page. We previously used a function that could be called which would remove any error artifacts from the session before the request was processed. This was however not applied consistently. This commit now introduces a middleware which will remove session data from the session at the start of each request.
1 parent c6e4fcd commit ae2714b

1 file changed

Lines changed: 18 additions & 10 deletions

File tree

lib/importer/src/index.js

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ exports.Initialise = (config, router, prototypeKit) => {
8282
// remember errors after they have been shown,
8383
//--------------------------------------------------------------------
8484
const cleanRequest = (request) => {
85-
delete request.session.data['reference_number'];
8685
delete request.session.data[IMPORTER_ERROR_KEY];
86+
delete request.session.data[IMPORTER_ERROR_DATA_KEY];
8787
delete request.session.data[IMPORTER_ERROR_EXTRA_KEY];
8888
};
8989

@@ -124,18 +124,28 @@ exports.Initialise = (config, router, prototypeKit) => {
124124
response.redirect(decodeURIComponent(request.query.next));
125125
};
126126

127+
128+
//--------------------------------------------------------------------
129+
// Clean any error message from the session. This is used to
130+
// ensure that we do not have any lingering error messages from
131+
// previous requests.
127132
//--------------------------------------------------------------------
128-
// Starts the upload process by clearing any lingering error messages
129-
// from the session. We can't rely on clearing _all_ of the session so
130-
// we are a bit more selective in what is deleted before redirecting
131-
// to the next step. This avoids us having to 'clear session data' each
132-
// time we run through the process.
133+
router.use((request, response, next) => {
134+
cleanRequest(request);
135+
next();
136+
})
137+
138+
//--------------------------------------------------------------------
139+
// Starts the upload process by removing any previous reference number
140+
// from the session, and then redirecting to the next URL.
141+
//
142+
// TODO: Decide if we want to delete the entire session object when we
143+
// start a new upload, or just the reference number.
133144
//--------------------------------------------------------------------
134145
router.all(
135146
IMPORTER_ROUTE_MAP.get("importerStartPath"),
136147
(request, response) => {
137-
138-
cleanRequest(request);
148+
delete request.session.data['reference_number'];
139149
redirectOnwards(request, response);
140150
},
141151
);
@@ -151,8 +161,6 @@ exports.Initialise = (config, router, prototypeKit) => {
151161
IMPORTER_ROUTE_MAP.get("importerUploadPath"),
152162
uploader.single("file"),
153163
(request, response) => {
154-
cleanRequest(request);
155-
156164
let createResponse = session_lib.CreateSession(plugin_config, request);
157165
if (createResponse.error) {
158166
request.session.data[IMPORTER_ERROR_KEY] = createResponse.error;

0 commit comments

Comments
 (0)