Skip to content
This repository was archived by the owner on Oct 2, 2019. It is now read-only.

Commit 1d7b112

Browse files
Throw a custom exception if calling provided transformer throws an exception (#37)
1 parent 7cf11f9 commit 1d7b112

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ plugins {
1010
}
1111

1212
group 'uk.gov.hmcts.reform'
13-
version '0.1.0'
13+
version '0.2.0'
1414

1515
sourceCompatibility = 1.8
1616

src/main/java/uk/gov/hmcts/reform/bulkscanccdeventhandler/handler/ExceptionRecordEventHandler.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import uk.gov.hmcts.reform.bulkscanccdeventhandler.ccd.CcdClient;
44
import uk.gov.hmcts.reform.bulkscanccdeventhandler.handler.exceptions.InvalidTransformationResultTypeException;
5+
import uk.gov.hmcts.reform.bulkscanccdeventhandler.handler.exceptions.TransformationException;
56
import uk.gov.hmcts.reform.bulkscanccdeventhandler.handler.model.CaseCreationRequest;
67
import uk.gov.hmcts.reform.bulkscanccdeventhandler.handler.model.CaseCreationResult;
78
import uk.gov.hmcts.reform.bulkscanccdeventhandler.handler.validation.CaseCreationRequestValidator;
@@ -36,8 +37,19 @@ public ExceptionRecordEventHandler(
3637
public CaseCreationResult handle(CaseCreationRequest req) {
3738
validator.validate(req);
3839

40+
final TransformationResult result;
41+
try {
42+
result = transformer.transform(req.exceptionRecord);
43+
} catch (Exception exc) {
44+
throw new TransformationException(
45+
"Provided transformer threw an exception when transforming exception record to a case. "
46+
+ "See cause for details.",
47+
exc
48+
);
49+
}
50+
3951
return handleTransformationResult(
40-
transformer.transform(req.exceptionRecord),
52+
result,
4153
okRes -> {
4254
if (okRes.warnings.isEmpty() || req.ignoreWarnings) {
4355
// TODO: handle exceptions
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package uk.gov.hmcts.reform.bulkscanccdeventhandler.handler.exceptions;
2+
3+
public class TransformationException extends RuntimeException {
4+
public TransformationException(String message, Throwable cause) {
5+
super(message, cause);
6+
}
7+
}

src/test/java/uk/gov/hmcts/reform/bulkscanccdeventhandler/handler/ExceptionRecordEventHandlerTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.mockito.Mock;
77
import org.mockito.junit.jupiter.MockitoExtension;
88
import uk.gov.hmcts.reform.bulkscanccdeventhandler.ccd.CcdClient;
9+
import uk.gov.hmcts.reform.bulkscanccdeventhandler.handler.exceptions.TransformationException;
910
import uk.gov.hmcts.reform.bulkscanccdeventhandler.handler.model.CaseCreationRequest;
1011
import uk.gov.hmcts.reform.bulkscanccdeventhandler.handler.model.CaseCreationResult;
1112
import uk.gov.hmcts.reform.bulkscanccdeventhandler.handler.validation.CaseCreationRequestValidator;
@@ -15,6 +16,7 @@
1516

1617
import static java.util.Arrays.asList;
1718
import static org.assertj.core.api.Assertions.assertThat;
19+
import static org.assertj.core.api.Assertions.catchThrowable;
1820
import static org.mockito.ArgumentMatchers.any;
1921
import static org.mockito.BDDMockito.given;
2022
import static org.mockito.Mockito.never;
@@ -134,4 +136,20 @@ public void should_validate_request() {
134136
// then
135137
verify(validator).validate(req);
136138
}
139+
140+
@Test
141+
void should_throw_custom_exception_when_provided_transformer_fails() {
142+
// given
143+
CaseCreationRequest req = SampleCaseCreationRequest.caseCreationRequest();
144+
RuntimeException cause = new RuntimeException();
145+
given(transformer.transform(req.exceptionRecord)).willThrow(cause);
146+
147+
// when
148+
Throwable exc = catchThrowable(() -> handler.handle(req));
149+
150+
//then
151+
assertThat(exc)
152+
.isInstanceOf(TransformationException.class)
153+
.hasCause(cause);
154+
}
137155
}

0 commit comments

Comments
 (0)