From a79a29356149ebba2b14d67c5f6b8791ffa82d4f Mon Sep 17 00:00:00 2001 From: Hugh FD Jackson Date: Fri, 17 Jan 2025 12:59:51 +0000 Subject: [PATCH 1/3] BAU: add explicit error logging from GOV notify --- src/server/common/connectors/notify/notify.js | 8 ++++-- .../common/connectors/notify/notify.test.js | 25 +++++++++++++++++-- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/server/common/connectors/notify/notify.js b/src/server/common/connectors/notify/notify.js index cf4d1744..8bdc2c18 100644 --- a/src/server/common/connectors/notify/notify.js +++ b/src/server/common/connectors/notify/notify.js @@ -26,9 +26,13 @@ export function sendNotification(data) { Authorization: 'Bearer ' + createToken(notifyConfig.apiKey) } } - ).then((response) => { + ).then(async (response) => { if (!response.ok) { - throw new Error() + const body = await response.json() + const errors = body.errors.map((error) => error.message) + throw new Error( + `HTTP failure from GOV.uk notify: status ${response.status} with the following errors: ${errors.join(', ')}` + ) } return response }) diff --git a/src/server/common/connectors/notify/notify.test.js b/src/server/common/connectors/notify/notify.test.js index 1ff52a98..5598f367 100644 --- a/src/server/common/connectors/notify/notify.test.js +++ b/src/server/common/connectors/notify/notify.test.js @@ -42,9 +42,30 @@ describe('sendNotification', () => { }) it('should throw an error if the response is not ok', async () => { - const mockResponse = { ok: false } + const mockResponse = { + ok: false, + status: 400, + + // eslint-disable-next-line @typescript-eslint/require-await + json: async () => ({ + status_code: 400, + errors: [ + { + error: 'BadRequestError', + message: "Can't send to this recipient using a team-only API key" + }, + { + error: 'BadRequestError', + message: + "Can't send to this recipient when service is in trial mode" + } + ] + }) + } mockProxyFetch.mockImplementation(() => Promise.resolve(mockResponse)) - await expect(sendNotification(testData)).rejects.toThrow() + await expect(sendNotification(testData)).rejects.toThrow( + "HTTP failure from GOV.uk notify: status 400 with the following errors: Can't send to this recipient using a team-only API key, Can't send to this recipient when service is in trial mode" + ) }) }) From c3301cfd298bcad0c45dd48f731b38f5b62de4ad Mon Sep 17 00:00:00 2001 From: Hugh FD Jackson Date: Fri, 17 Jan 2025 13:18:15 +0000 Subject: [PATCH 2/3] BAU: body -> responseBody --- src/server/common/connectors/notify/notify.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/server/common/connectors/notify/notify.js b/src/server/common/connectors/notify/notify.js index 8bdc2c18..5d4f08a6 100644 --- a/src/server/common/connectors/notify/notify.js +++ b/src/server/common/connectors/notify/notify.js @@ -28,8 +28,8 @@ export function sendNotification(data) { } ).then(async (response) => { if (!response.ok) { - const body = await response.json() - const errors = body.errors.map((error) => error.message) + const responseBody = await response.json() + const errors = responseBody.errors.map((error) => error.message) throw new Error( `HTTP failure from GOV.uk notify: status ${response.status} with the following errors: ${errors.join(', ')}` ) From 2e3d52e28be3dabdddb9860e60b25cdf97a85abc Mon Sep 17 00:00:00 2001 From: Hugh FD Jackson Date: Fri, 17 Jan 2025 13:37:26 +0000 Subject: [PATCH 3/3] BAU: refactor to async function --- src/server/common/connectors/notify/notify.js | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/server/common/connectors/notify/notify.js b/src/server/common/connectors/notify/notify.js index 5d4f08a6..abdfe166 100644 --- a/src/server/common/connectors/notify/notify.js +++ b/src/server/common/connectors/notify/notify.js @@ -10,14 +10,14 @@ const notifyConfig = config.get('notify') /** * @param {NotifyContent} data */ -export function sendNotification(data) { +export async function sendNotification(data) { const body = JSON.stringify({ template_id: notifyConfig.templateId, email_address: notifyConfig.caseDeliveryEmailAddress, personalisation: data }) - return proxyFetch( + const response = await proxyFetch( 'https://api.notifications.service.gov.uk/v2/notifications/email', { method: 'POST', @@ -26,14 +26,14 @@ export function sendNotification(data) { Authorization: 'Bearer ' + createToken(notifyConfig.apiKey) } } - ).then(async (response) => { - if (!response.ok) { - const responseBody = await response.json() - const errors = responseBody.errors.map((error) => error.message) - throw new Error( - `HTTP failure from GOV.uk notify: status ${response.status} with the following errors: ${errors.join(', ')}` - ) - } - return response - }) + ) + + if (!response.ok) { + const responseBody = await response.json() + const errors = responseBody.errors.map((error) => error.message) + throw new Error( + `HTTP failure from GOV.uk notify: status ${response.status} with the following errors: ${errors.join(', ')}` + ) + } + return response }