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', {