Skip to content

Commit

Permalink
Merge pull request #135 from DEFRA/BAU-improve-logging-for-gov-notify…
Browse files Browse the repository at this point in the history
…-integration

BAU: add explicit error logging from GOV notify
  • Loading branch information
hughfdjackson authored Jan 17, 2025
2 parents e647fa1 + 2e3d52e commit 17d50a2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
20 changes: 12 additions & 8 deletions src/server/common/connectors/notify/notify.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -26,10 +26,14 @@ export function sendNotification(data) {
Authorization: 'Bearer ' + createToken(notifyConfig.apiKey)
}
}
).then((response) => {
if (!response.ok) {
throw new Error()
}
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
}
25 changes: 23 additions & 2 deletions src/server/common/connectors/notify/notify.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
})
})

0 comments on commit 17d50a2

Please sign in to comment.