public ApprenticeshipApplicationViewModel CreateApplicationViewModel(Guid candidateId, int vacancyId) { _logger.Debug( "Calling ApprenticeshipApplicationProvider to get the Application View Model for candidate ID: {0}, vacancy ID: {1}.", candidateId, vacancyId); try { var applicationDetails = _candidateService.CreateApplication(candidateId, vacancyId); if (applicationDetails == null) { return(new ApprenticeshipApplicationViewModel { Status = ApplicationStatuses.ExpiredOrWithdrawn, ViewModelMessage = MyApplicationsPageMessages.ApprenticeshipNoLongerAvailable }); } _candidateApplicationsProvider.RecalculateSavedAndDraftCount(candidateId, null); var applicationViewModel = _apprenticeshipCandidateWebMappers.Map <ApprenticeshipApplicationDetail, ApprenticeshipApplicationViewModel>(applicationDetails); return(PatchWithVacancyDetail(candidateId, vacancyId, applicationViewModel)); } catch (CustomException e) { if (e.Code == ErrorCodes.EntityStateError) { _logger.Info(e.Message, e); return (new ApprenticeshipApplicationViewModel(MyApplicationsPageMessages.ApplicationInIncorrectState, ApplicationViewModelStatus.ApplicationInIncorrectState)); } var message = string.Format( "Unhandled custom exception while getting the Application View Model for candidate ID: {0}, vacancy ID: {1}.", candidateId, vacancyId); _logger.Error(message, e); return(new ApprenticeshipApplicationViewModel("Unhandled error", ApplicationViewModelStatus.Error)); } catch (Exception e) { var message = string.Format("Get Application View Model failed for candidate ID: {0}, vacancy ID: {1}.", candidateId, vacancyId); _logger.Error(message, e); return (new ApprenticeshipApplicationViewModel( MyApplicationsPageMessages.CreateOrRetrieveApplicationFailed, ApplicationViewModelStatus.Error)); } }