From f3920ca194dcccf2bf0d000dc53e1e64078827f6 Mon Sep 17 00:00:00 2001 From: Daniel Liburd Date: Wed, 20 Sep 2023 11:08:48 +0100 Subject: [PATCH] Render guidance if we fail to fetch offences Previously, if we received a 404 when fetching the offences for a PoP we'd render the generic error page. As we've identified this scenario to be caused by a missing NOMS number for the CRN, we want to show guidance to the user in how to correct it. This commit conditionally renders the missing NOMS guidance page if we receive a 404 when fetching offences. --- .../apply/applicationsController.test.ts | 15 +++++++++ .../apply/applicationsController.ts | 33 ++++++++++++------- 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/server/controllers/apply/applicationsController.test.ts b/server/controllers/apply/applicationsController.test.ts index 6517bbd51..e790d8a37 100644 --- a/server/controllers/apply/applicationsController.test.ts +++ b/server/controllers/apply/applicationsController.test.ts @@ -14,6 +14,7 @@ import paths from '../../paths/apply' import { firstPageOfApplicationJourney, getResponses } from '../../utils/applicationUtils' import { DateFormats } from '../../utils/dateUtils' import extractCallConfig from '../../utils/restUtils' +import { SanitisedError } from '../../sanitisedError' jest.mock('../../utils/validation') jest.mock('../../utils/applicationUtils') @@ -133,6 +134,20 @@ describe('applicationsController', () => { personService.getOffences.mockResolvedValue([offence]) }) + describe('if we fail to fetch offences', () => { + it('should render guidance on missing NOMS number', async () => { + const err = { data: { status: 404 } } + personService.getOffences.mockImplementation(() => { + throw err + }) + const requestHandler = applicationsController.new() + + await requestHandler(request, response, next) + + expect(response.render).toHaveBeenCalledWith('applications/people/missingNoms') + }) + }) + describe('if an error has not been sent to the flash', () => { beforeEach(() => { ;(fetchErrorsAndUserInput as jest.Mock).mockImplementation(() => { diff --git a/server/controllers/apply/applicationsController.ts b/server/controllers/apply/applicationsController.ts index 4e7fcf2e4..ca7abd57d 100644 --- a/server/controllers/apply/applicationsController.ts +++ b/server/controllers/apply/applicationsController.ts @@ -58,19 +58,28 @@ export default class ApplicationsController { if (crnArr.length) { const crn = crnArr[0] const person = await this.personService.findByCrn(callConfig, crn) - const offences = await this.personService.getOffences(callConfig, crn) - const offenceId = offences.length === 1 ? offences[0].offenceId : null - - return res.render(`applications/people/confirm`, { - person, - date: DateFormats.dateObjtoUIDate(new Date()), - crn, - offenceId, - errors, - errorSummary, - ...userInput, - }) + try { + const offences = await this.personService.getOffences(callConfig, crn) + + const offenceId = offences.length === 1 ? offences[0].offenceId : null + + return res.render(`applications/people/confirm`, { + person, + date: DateFormats.dateObjtoUIDate(new Date()), + crn, + offenceId, + errors, + errorSummary, + ...userInput, + }) + } catch (e) { + if (e?.data?.status === 404) { + return res.render('applications/people/missingNoms') + } + + throw e + } } return res.render('applications/new', {