Skip to content

Commit

Permalink
Clean up errors on each request
Browse files Browse the repository at this point in the history
  • Loading branch information
rossjones committed Sep 11, 2024
1 parent a7ade71 commit 1998448
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 17 deletions.
41 changes: 31 additions & 10 deletions lib/importer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,38 @@ exports.Initialise = (config, router, prototypeKit) => {
// one created by us.
config.uploadPath ??= path.join(os.tmpdir(), 'reg-dyn-importer'); ;


//--------------------------------------------------------------------
// Removes any previous importer error from the session. When we set
// an error we redirect to the referer and we expect that page to show
// the error. Calling this in each POST endpoint ensures that we don't
// remember errors after they have been shown,
//--------------------------------------------------------------------
const cleanRequest = (request) => {
router.all("*", (request, res, next) => {
delete request.session.data[IMPORTER_ERROR_KEY]
}
next();
});


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

prototypeKit.views.addFunction(k, (next)=>{
return `${v}?next=${encodeURIComponent(next)}`;
prototypeKit.views.addFunction(k, (next, errorPage=null)=>{
let url = `${v}?next=${encodeURIComponent(next)}`;
if (errorPage) {
url = url + `&error=${encodeURIComponent(errorPage)}`
}
return url
}, {})
}

Expand Down Expand Up @@ -140,8 +150,13 @@ exports.Initialise = (config, router, prototypeKit) => {
// Redirects the current request to the 'next' URL after decoding the
// encoded URI.
//--------------------------------------------------------------------
const redirectOnwards = (request, response) => {
response.redirect(decodeURIComponent(request.query.next));
const redirectOnwards = (request, response, failed=false) => {
console.log(request.query)
if (failed && request.query.error) {
response.redirect(decodeURIComponent(request.query.error));
} else {
response.redirect(decodeURIComponent(request.query.next));
}
}

//--------------------------------------------------------------------
Expand All @@ -152,7 +167,6 @@ exports.Initialise = (config, router, prototypeKit) => {
// time we run through the process.
//--------------------------------------------------------------------
router.all(IMPORTER_ROUTE_MAP.get("importerStartPath"),(request, response) => {
cleanRequest(request);
redirectOnwards(request, response);
});

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

router.post(IMPORTER_ROUTE_MAP.get("importerUploadPath"), uploader.single("file"), (request, response) => {
cleanRequest(request);

let createResponse = session_lib.CreateSession(config, request);

if (createResponse.error) {
Expand Down Expand Up @@ -274,6 +286,15 @@ exports.Initialise = (config, router, prototypeKit) => {
}

session.mapping = request.body;
if (Object.values(session.mapping).every((v) => v=='') ) {
request.session.data[IMPORTER_ERROR_KEY] = "No columns were mapped to the expected fields"
if (!request.query.error) {
response.redirect(request.get('Referrer'))
} else {
redirectOnwards(request, response, failed=true);
}
return;
}

// Ensure the session is persisted. Currently in session, eventually another way
request.session.data[IMPORTER_SESSION_KEY] = session;
Expand Down
24 changes: 18 additions & 6 deletions lib/importer/nunjucks/importer/macros/field_mapper.njk
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,25 @@
{% macro importerFieldMapper(data, caption='', columnTitle='Column', examplesTitle='Example values', fieldsTitle='Fields') %}
{% set fields = data['importer.session']['fields'] %}
{% set headings = importerGetHeaders(data) %}
{% set error = headings.error %}
{% set error = importerError(data) %}

{% if error %}
<p id="mapping-error" class="govuk-error-message">
<span class="govuk-visually-hidden">Error:</span> {{ error.text }}
</p>
{% endif %}

{% if error %}
<div class="govuk-error-summary" data-module="govuk-error-summary">
<div role="alert">
<h2 class="govuk-error-summary__title">
There was a problem submitting this data
</h2>
<div class="govuk-error-summary__body">
<ul class="govuk-list govuk-error-summary__list">
<li>
{{ error.text }}
</li>
</ul>
</div>
</div>
</div>
{% endif %}


<table class="govuk-table">
Expand Down
2 changes: 1 addition & 1 deletion prototypes/basic/app/views/mapping.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ <h1 class="govuk-heading-l">Map columns</h1>

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

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

{{ importerFieldMapper(data) }}

Expand Down

0 comments on commit 1998448

Please sign in to comment.