diff --git a/src/middleware/issueDetails.middleware.js b/src/middleware/issueDetails.middleware.js index 2b11f3fa..4bbd24b0 100644 --- a/src/middleware/issueDetails.middleware.js +++ b/src/middleware/issueDetails.middleware.js @@ -24,6 +24,7 @@ import { import { fetchIf, renderTemplate } from './middleware.builders.js' import * as v from 'valibot' import escape from 'escape-html' +import logger from '../utils/logger.js' export const IssueDetailsQueryParams = v.strictObject({ lpa: v.string(), @@ -44,10 +45,12 @@ const validateIssueDetailsQueryParams = validateQueryParams({ * @param {{value: string}?} issue * @returns {string} */ -export const issueErrorMessageHtml = (errorMessage, issue) => - `
${ - escape(issue ? issue.value ?? '' : '') - }` +export const issueErrorMessageHtml = (errorMessage, issue) => { + if (!errorMessage) return '' + return ` ${ + escape(issue ? issue.value ?? '' : '') + }` +} /** * @@ -111,7 +114,10 @@ export function prepareIssueDetailsTemplateParams (req, res, next) { let classes = '' const fieldValue = entity[datasetField] || { value: '' } if (fieldValue.issue) { - valueHtml += issueErrorMessageHtml(entity[datasetField].issue.message, null) + if (!fieldValue.issue.message) { + logger.warn('no issue message found for issue in entity', { entity }) + } + valueHtml += issueErrorMessageHtml(fieldValue.issue.message, null) classes += 'dl-summary-card-list__row--error' } valueHtml += escape(entity[datasetField]?.value || '') diff --git a/test/unit/middleware/issueDetails.middleware.test.js b/test/unit/middleware/issueDetails.middleware.test.js index 2adfaa3d..abf43ca4 100644 --- a/test/unit/middleware/issueDetails.middleware.test.js +++ b/test/unit/middleware/issueDetails.middleware.test.js @@ -27,6 +27,12 @@ describe('issueDetails.middleware.js', () => { const result = issueErrorMessageHtml(errorMessage, issue) expect(result).toBe(` `) }) + + it('should return an empty string if errorMessage is null', () => { + const issue = { value: '02-02-2022' } + const result = issueErrorMessageHtml(null, issue) + expect(result).toBe('') + }) }) describe('getIssueField', () => {