From 159b9bc19a01b05a62c9f83d9740ce35b5401f8c Mon Sep 17 00:00:00 2001 From: Roland Sadowski Date: Mon, 16 Dec 2024 14:31:25 +0000 Subject: [PATCH] fix: uncaught exception in case of missing source --- src/middleware/datasetOverview.middleware.js | 6 +++--- test/unit/middleware/datasetOverview.middleware.test.js | 8 ++++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/middleware/datasetOverview.middleware.js b/src/middleware/datasetOverview.middleware.js index 373b9785..3940147a 100644 --- a/src/middleware/datasetOverview.middleware.js +++ b/src/middleware/datasetOverview.middleware.js @@ -126,12 +126,12 @@ export const setNoticesFromSourceKey = (sourceKey) => (req, res, next) => { const { deadlineDate, lastYearDeadline, twoYearsAgoDeadline } = getDeadlineHistory(deadlineObj.deadline) - const startDate = new Date(source.startDate) + const startDate = source ? new Date(source.startDate) : undefined - if (startDate.toString() === 'Invalid Date') { + if (!startDate || startDate.toString() === 'Invalid Date') { logger.warn('Invalid start date encountered', { type: types.DataValidation, - startDate: source.startDate + startDate: source?.startDate }) return next() } diff --git a/test/unit/middleware/datasetOverview.middleware.test.js b/test/unit/middleware/datasetOverview.middleware.test.js index 50c57d4d..960f4420 100644 --- a/test/unit/middleware/datasetOverview.middleware.test.js +++ b/test/unit/middleware/datasetOverview.middleware.test.js @@ -203,5 +203,13 @@ describe('Dataset Overview Middleware', () => { expect(reqWithDataset.notice).toBeUndefined() }) + + it('should handle missing source key gracefully', () => { + const reqWithoutSource = { + ...req + } + setNoticesFromSourceKey('foobar')(reqWithoutSource, res, () => {}) + expect(reqWithoutSource.notice).toBeUndefined() + }) }) })