From 0b56e351dbe49897b53501e503fca1d035a7fbae Mon Sep 17 00:00:00 2001 From: Roland Sadowski Date: Thu, 11 Jul 2024 09:58:15 +0100 Subject: [PATCH 1/6] change default PORT to 8001 5000 conflicts with macOS service --- config/default.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/default.yaml b/config/default.yaml index 1668607d..c482c6ba 100644 --- a/config/default.yaml +++ b/config/default.yaml @@ -1,4 +1,4 @@ -port: 5000 +port: 8001 asyncRequestApi: { url: https://publish-api.planning.data.gov.uk, port: 8082, From c263f912f97e2f3e8932f334fe13de1e598e60ee Mon Sep 17 00:00:00 2001 From: Roland Sadowski Date: Thu, 11 Jul 2024 09:59:58 +0100 Subject: [PATCH 2/6] better logging on Redis connection error --- src/serverSetup/session.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/serverSetup/session.js b/src/serverSetup/session.js index d3b863d1..d5d4f47d 100644 --- a/src/serverSetup/session.js +++ b/src/serverSetup/session.js @@ -13,7 +13,10 @@ export function setupSession (app) { const redisClient = createClient({ url: `${urlPrefix}://${config.redis.host}:${config.redis.port}` }) - redisClient.connect().catch(logger.error) + const errorHandler = (err) => { + logger.error(`redis connection error ${err.code}`) + } + redisClient.connect().catch(errorHandler) sessionStore = new RedisStore({ client: redisClient From bc2ba3eabae8f1d918b670b5b0731290c1ba3621 Mon Sep 17 00:00:00 2001 From: Roland Sadowski Date: Thu, 11 Jul 2024 10:12:24 +0100 Subject: [PATCH 3/6] remove unnecessary async/await --- src/serverSetup/middlewares.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/serverSetup/middlewares.js b/src/serverSetup/middlewares.js index f2a617a1..5286e576 100644 --- a/src/serverSetup/middlewares.js +++ b/src/serverSetup/middlewares.js @@ -6,13 +6,13 @@ import hash from '../utils/hasher.js' import config from '../../config/index.js' export function setupMiddlewares (app) { - app.use(async (req, res, next) => { + app.use((req, res, next) => { logger.info({ type: 'Request', method: req.method, endpoint: req.originalUrl, message: `${req.method} request made to ${req.originalUrl}`, - sessionId: await hash(req.sessionID) + sessionId: hash(req.sessionID) }) next() }) From 0563a3a86a4332cdb620a8dbcf6174f208d1e385 Mon Sep 17 00:00:00 2001 From: Roland Sadowski Date: Thu, 11 Jul 2024 12:47:34 +0100 Subject: [PATCH 4/6] less noisy test output --- src/routes/health.js | 6 +++++- src/serverSetup/session.js | 2 +- src/utils/fetchLocalAuthorities.js | 7 ++++++- test/unit/health.test.js | 2 +- 4 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/routes/health.js b/src/routes/health.js index fab987e2..3d1ccb76 100644 --- a/src/routes/health.js +++ b/src/routes/health.js @@ -2,6 +2,7 @@ import express from 'express' import config from '../../config/index.js' import AWS from 'aws-sdk' import { createClient } from 'redis' +import logger from '../utils/logger.js' const router = express.Router() @@ -71,7 +72,10 @@ const checkRedis = async () => { return false } }).catch((err) => { - console.log('error:', err) + logger.error(`checkRedis/connect: ${err.message}`) + if (config.environment !== 'test') { + console.error(err) + } return false }) } diff --git a/src/serverSetup/session.js b/src/serverSetup/session.js index d5d4f47d..02b1e9c3 100644 --- a/src/serverSetup/session.js +++ b/src/serverSetup/session.js @@ -14,7 +14,7 @@ export function setupSession (app) { url: `${urlPrefix}://${config.redis.host}:${config.redis.port}` }) const errorHandler = (err) => { - logger.error(`redis connection error ${err.code}`) + logger.error(`session/setupSession: redis connection error: ${err.code}`) } redisClient.connect().catch(errorHandler) diff --git a/src/utils/fetchLocalAuthorities.js b/src/utils/fetchLocalAuthorities.js index 5fb29790..277e7114 100644 --- a/src/utils/fetchLocalAuthorities.js +++ b/src/utils/fetchLocalAuthorities.js @@ -1,4 +1,6 @@ import axios from 'axios' +import logger from './logger' +import config from '../../config' /** * Fetches a list of local authority names from a specified dataset. @@ -36,7 +38,10 @@ export const fetchLocalAuthorities = async () => { }).filter(name => name !== null) // Filter out null values return names // Return the fetched data } catch (error) { - console.error('Error fetching local authorities data:', error) + logger.error(`fetchLocalAuthorities: Error fetching local authorities data: ${error.message}`) + if (config.environment !== 'test') { + console.error(error) + } throw error // Rethrow the error to be handled by the caller } } diff --git a/test/unit/health.test.js b/test/unit/health.test.js index cdee0fd7..38c59d62 100644 --- a/test/unit/health.test.js +++ b/test/unit/health.test.js @@ -52,7 +52,7 @@ describe('Health checks', () => { test('checkRedis returns false when Redis is not reachable', async () => { const mockClient = { - connect: vi.fn().mockRejectedValue(new Error()), + connect: vi.fn().mockRejectedValue(new Error('redis not reachable!')), isOpen: false, quit: vi.fn() } From 67acfa983985afc1eb402169a9354ed4703c2940 Mon Sep 17 00:00:00 2001 From: Roland Sadowski Date: Thu, 11 Jul 2024 13:41:50 +0100 Subject: [PATCH 5/6] change default port to 8011 --- config/default.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/default.yaml b/config/default.yaml index c482c6ba..aeb8855d 100644 --- a/config/default.yaml +++ b/config/default.yaml @@ -1,4 +1,4 @@ -port: 8001 +port: 8011 asyncRequestApi: { url: https://publish-api.planning.data.gov.uk, port: 8082, From 56e5f0563fae97bb9d930729eca71df278e0c614 Mon Sep 17 00:00:00 2001 From: Roland Sadowski Date: Thu, 11 Jul 2024 14:37:17 +0100 Subject: [PATCH 6/6] update imports to fix tests --- src/utils/fetchLocalAuthorities.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/utils/fetchLocalAuthorities.js b/src/utils/fetchLocalAuthorities.js index 277e7114..3891ce0f 100644 --- a/src/utils/fetchLocalAuthorities.js +++ b/src/utils/fetchLocalAuthorities.js @@ -1,6 +1,6 @@ import axios from 'axios' -import logger from './logger' -import config from '../../config' +import logger from '../../src/utils/logger.js' +import config from '../../config/index.js' /** * Fetches a list of local authority names from a specified dataset. @@ -36,12 +36,12 @@ export const fetchLocalAuthorities = async () => { return row[1] } }).filter(name => name !== null) // Filter out null values - return names // Return the fetched data + return names } catch (error) { logger.error(`fetchLocalAuthorities: Error fetching local authorities data: ${error.message}`) if (config.environment !== 'test') { console.error(error) } - throw error // Rethrow the error to be handled by the caller + throw error } }