Skip to content

Commit

Permalink
Add new exception for markdown errors.
Browse files Browse the repository at this point in the history
We probably shouldn't use an exception provided by a dependency for this.
  • Loading branch information
tom-saunders-cts committed Jan 9, 2025
1 parent 1bbf8e9 commit 773aab9
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import uk.gov.hmcts.probate.config.properties.registries.Registry;
import uk.gov.hmcts.probate.exception.BadRequestException;
import uk.gov.hmcts.probate.exception.InvalidEmailException;
import uk.gov.hmcts.probate.exception.RequestInformationParameterException;
import uk.gov.hmcts.probate.insights.AppInsights;
import uk.gov.hmcts.probate.model.ApplicationType;
import uk.gov.hmcts.probate.model.CaseType;
Expand Down Expand Up @@ -2124,7 +2125,7 @@ void verifySendCaveatNocEmail()

@Test
void throwExceptionSendEmailWhenInvalidPersonalisationExists() {
NotificationClientException expectException = assertThrows(NotificationClientException.class,
RequestInformationParameterException expectException = assertThrows(RequestInformationParameterException.class,
() -> notificationService.sendEmail(CASE_STOPPED, markdownLinkCaseData));
assertEquals(MARKDOWN_ERROR_MESSAGE, expectException.getMessage());
}
Expand All @@ -2140,14 +2141,14 @@ void throwExceptionSendExecutorEmailWhenInvalidPersonalisationExists() {
.build())
.email("[email protected]")
.notification("Yes").build();
NotificationClientException expectException = assertThrows(NotificationClientException.class,
RequestInformationParameterException expectException = assertThrows(RequestInformationParameterException.class,
() -> notificationService.sendEmail(CASE_STOPPED_REQUEST_INFORMATION, markdownLinkCaseData));
assertEquals(MARKDOWN_ERROR_MESSAGE, expectException.getMessage());
}

@Test
void throwExceptionSendCaveatEmailWhenInvalidPersonalisationExists() {
NotificationClientException expectException = assertThrows(NotificationClientException.class,
RequestInformationParameterException expectException = assertThrows(RequestInformationParameterException.class,
() -> notificationService.sendCaveatEmail(GENERAL_CAVEAT_MESSAGE, markdownLinkCaveatData));
assertEquals(MARKDOWN_ERROR_MESSAGE, expectException.getMessage());
}
Expand All @@ -2166,7 +2167,7 @@ void throwExceptionSendEmailWithDocumentAttachedWhenInvalidPersonalisationExists
.build())
.email("[email protected]")
.notification("Yes").build();
NotificationClientException expectException = assertThrows(NotificationClientException.class,
RequestInformationParameterException expectException = assertThrows(RequestInformationParameterException.class,
() -> notificationService.sendEmailWithDocumentAttached(markdownLinkCaseData,
executorsApplyingNotification, REDECLARATION_SOT));
assertEquals(MARKDOWN_ERROR_MESSAGE, expectException.getMessage());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package uk.gov.hmcts.probate.exception;

public class RequestInformationParameterException extends RuntimeException {
private static final String INVALID_PERSONALISATION_ERROR_MESSAGE =
"Markdown Link detected in case data, stop sending notification email.";

public RequestInformationParameterException() {
super(INVALID_PERSONALISATION_ERROR_MESSAGE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import uk.gov.hmcts.probate.exception.ConnectionException;
import uk.gov.hmcts.probate.exception.NotFoundException;
import uk.gov.hmcts.probate.exception.OCRMappingException;
import uk.gov.hmcts.probate.exception.RequestInformationParameterException;
import uk.gov.hmcts.probate.exception.SocketException;
import uk.gov.hmcts.probate.exception.model.ErrorResponse;
import uk.gov.hmcts.probate.model.ccd.ocr.ValidationResponse;
Expand Down Expand Up @@ -41,7 +42,6 @@ class DefaultExceptionHandler extends ResponseEntityExceptionHandler {
public static final String CONNECTION_ERROR = "Connection error";
public static final String UNAUTHORISED_DATA_EXTRACT_ERROR = "Unauthorised access to Data-Extract error";


@ExceptionHandler(BadRequestException.class)
public ResponseEntity<ErrorResponse> handle(BadRequestException exception) {

Expand Down Expand Up @@ -93,6 +93,22 @@ public ResponseEntity<ErrorResponse> handle(ConnectionException exception) {
return new ResponseEntity<>(errorResponse, headers, SERVICE_UNAVAILABLE);
}

@ExceptionHandler(value = RequestInformationParameterException.class)
public ResponseEntity<CallbackResponse> handle(RequestInformationParameterException exception) {
log.warn("Invalid parameters when sending email", exception);

final List<String> errors = List.of(exception.getMessage());
final CallbackResponse errorResponse = CallbackResponse.builder()
.errors(errors)
.build();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);

return ResponseEntity.ok()
.headers(headers)
.body(errorResponse);
}

@ExceptionHandler(value = NotificationClientException.class)
public ResponseEntity<ErrorResponse> handle(NotificationClientException exception) {
log.warn("Notification service exception", exception);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import uk.gov.hmcts.probate.config.properties.registries.Registry;
import uk.gov.hmcts.probate.exception.BadRequestException;
import uk.gov.hmcts.probate.exception.InvalidEmailException;
import uk.gov.hmcts.probate.exception.RequestInformationParameterException;
import uk.gov.hmcts.probate.model.ApplicationType;
import uk.gov.hmcts.probate.model.CaseOrigin;
import uk.gov.hmcts.probate.model.Constants;
Expand Down Expand Up @@ -71,8 +72,6 @@ public class NotificationService {
private static final String PERSONALISATION_APPLICANT_NAME = "applicant_name";
private static final String PERSONALISATION_SOT_LINK = "sot_link";
private static final DateTimeFormatter RELEASE_DATE_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd");
private static final String INVALID_PERSONALISATION_ERROR_MESSAGE =
"Markdown Link detected in case data, stop sending notification email.";

private final EmailAddresses emailAddresses;
private final NotificationTemplates notificationTemplates;
Expand Down Expand Up @@ -555,7 +554,7 @@ private String removedSolicitorNameForPersonalisation(CaseData caseData) {

CommonNotificationResult doCommonNotificationServiceHandling(
final Map<String, ?> personalisation,
final Long caseId) throws NotificationClientException {
final Long caseId) throws RequestInformationParameterException {
final PersonalisationValidationRule.PersonalisationValidationResult validationResult =
personalisationValidationRule.validatePersonalisation(personalisation);
final Map<String, String> invalidFields = validationResult.invalidFields();
Expand All @@ -564,7 +563,7 @@ CommonNotificationResult doCommonNotificationServiceHandling(
if (!invalidFields.isEmpty()) {
log.error("Personalisation validation failed for case: {} fields: {}",
caseId, invalidFields);
throw new NotificationClientException(INVALID_PERSONALISATION_ERROR_MESSAGE);
throw new RequestInformationParameterException();
} else if (!htmlFields.isEmpty()) {
log.info("Personalisation validation found HTML for case: {} fields: {}",
caseId, validationResult.htmlFields());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import uk.gov.hmcts.probate.exception.ConnectionException;
import uk.gov.hmcts.probate.exception.NotFoundException;
import uk.gov.hmcts.probate.exception.OCRMappingException;
import uk.gov.hmcts.probate.exception.RequestInformationParameterException;
import uk.gov.hmcts.probate.exception.SocketException;
import uk.gov.hmcts.probate.exception.model.ErrorResponse;
import uk.gov.hmcts.probate.exception.model.FieldErrorResponse;
Expand All @@ -21,6 +22,7 @@
import java.util.Arrays;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.openMocks;
import static org.springframework.http.HttpStatus.BAD_REQUEST;
Expand Down Expand Up @@ -178,4 +180,16 @@ void shouldReturnOCRMappingException() {
assertEquals(1, response.getBody().getErrors().size());
assertEquals("Message", response.getBody().getErrors().get(0));
}

@Test
void shouldReturnMarkdownError() {
RequestInformationParameterException ex = mock(RequestInformationParameterException.class);
when(ex.getMessage()).thenReturn(EXCEPTION_MESSAGE);

ResponseEntity<CallbackResponse> response = underTest.handle(ex);

assertEquals(OK, response.getStatusCode(), "Expected HTTP OK from RequestInformationParameterException handler");
assertEquals(1, response.getBody().getErrors().size(), "Expected one error");
assertEquals(EXCEPTION_MESSAGE, response.getBody().getErrors().get(0), "Expected error to be extracted from exception");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import uk.gov.hmcts.probate.config.notifications.EmailAddresses;
import uk.gov.hmcts.probate.config.notifications.NotificationTemplates;
import uk.gov.hmcts.probate.config.properties.registries.RegistriesProperties;
import uk.gov.hmcts.probate.exception.RequestInformationParameterException;
import uk.gov.hmcts.probate.service.documentmanagement.DocumentManagementService;
import uk.gov.hmcts.probate.service.notification.CaveatPersonalisationService;
import uk.gov.hmcts.probate.service.notification.GrantOfRepresentationPersonalisationService;
Expand All @@ -22,7 +23,6 @@
import uk.gov.hmcts.probate.validator.PersonalisationValidationRule.PersonalisationValidationResult;
import uk.gov.hmcts.reform.authorisation.generators.AuthTokenGenerator;
import uk.gov.service.notify.NotificationClient;
import uk.gov.service.notify.NotificationClientException;

import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -98,12 +98,13 @@ void givenPersonalisationWithMarkdown_whenCommonValidation_thenThrows() {
when(personalisationValidationRuleMock.validatePersonalisation(dummyPersonalisation))
.thenReturn(mockResult);

assertThrows(NotificationClientException.class, () ->
assertThrows(RequestInformationParameterException.class, () ->
notificationService.doCommonNotificationServiceHandling(dummyPersonalisation, dummyCaseId));
}

@Test
void givenPersonalisationWithHtml_whenCommonValidation_thenReturnsHtmlFound() throws NotificationClientException {
void givenPersonalisationWithHtml_whenCommonValidation_thenReturnsHtmlFound()
throws RequestInformationParameterException {
final Map<String, ?> dummyPersonalisation = Collections.emptyMap();
final Long dummyCaseId = 1L;

Expand All @@ -120,7 +121,8 @@ void givenPersonalisationWithHtml_whenCommonValidation_thenReturnsHtmlFound() th
}

@Test
void givenPersonalisationWithNoIssue_whenCommonValidation_thenReturnsAllOk() throws NotificationClientException {
void givenPersonalisationWithNoIssue_whenCommonValidation_thenReturnsAllOk()
throws RequestInformationParameterException {
final Map<String, ?> dummyPersonalisation = Collections.emptyMap();
final Long dummyCaseId = 1L;

Expand Down

0 comments on commit 773aab9

Please sign in to comment.