Skip to content

Commit

Permalink
Added support for unmapped exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
holgerknoche committed Mar 27, 2024
1 parent 7a838fb commit 3368626
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,15 @@ public R invokeOperation(P parameterObject, OnUnrepresentableValue<?> onUnrepres
String exceptionTypeName = determineSpecificTypeId(responseNode).orElseThrow(NoSuchElementException::new);
Class<?> exceptionTypeRepresentation = this.exceptionTypeMap.get(exceptionTypeName);

MappedExceptionData exceptionData = (MappedExceptionData) objectMapper.treeToValue(responseNode, exceptionTypeRepresentation);
throw exceptionData.createMappedException();
if (exceptionTypeRepresentation != null) {
// If the exception type is mapped, create the appropriate exception
MappedExceptionData exceptionData = (MappedExceptionData) objectMapper.treeToValue(responseNode, exceptionTypeRepresentation);
throw exceptionData.createMappedException();
} else {
// Otherwise, treat the exception as an unrepresentable value
JsonNode resultNode = onUnrepresentableValue.throwExceptionOrReturnDefaultNode();
return objectMapper.treeToValue(resultNode, this.resultTypeRepresentation);
}
} else {
// Otherwise, return the value
return objectMapper.treeToValue(responseNode, this.resultTypeRepresentation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import gutta.apievolution.json.consumer.ConsumerSuperType;
import gutta.apievolution.json.consumer.ConsumerTestException;
import gutta.apievolution.json.consumer.ConsumerTestExceptionData;
import gutta.apievolution.json.consumer.UnrepresentableValueException;
import gutta.apievolution.json.provider.MappableProviderTestException;
import gutta.apievolution.json.provider.ProviderEnum;
import gutta.apievolution.json.provider.ProviderMonoToPolySubTypeA;
Expand Down Expand Up @@ -175,6 +176,18 @@ void embeddedMonoToPolyTypeMapping() {
assertNotSame(parameter, result);
assertEquals(parameter, result);
}

/**
* Test case: The provider throws an exception, but the consumer does not expect one. This results in an unrepresentable value.
*/
@Test
void providerThrowsExceptionButConsumerDoesNotExpectOne() {
OpWithUnmappedExceptionProviderProxy providerProxy = new OpWithUnmappedExceptionProviderProxy();
RequestRouter requestRouter = new SimpleJsonRequestRouter(providerProxy);

OpWithUnmappedExceptionConsumerProxy consumerProxy = new OpWithUnmappedExceptionConsumerProxy(requestRouter);
assertThrows(UnrepresentableValueException.class, () -> consumerProxy.invokeOperation(new ConsumerParameter()));
}

private abstract static class ConsumerOperationProxyTemplate<P, R> extends ConsumerOperationProxy<P, R> {

Expand Down Expand Up @@ -308,6 +321,30 @@ protected ProviderResult invokeOperation(ProviderParameter parameter) {

}

private static class OpWithUnmappedExceptionConsumerProxy extends ConsumerOperationProxyTemplate<ConsumerParameter, ConsumerResult> {

public OpWithUnmappedExceptionConsumerProxy(RequestRouter router) {
super("opWithUnmappedException", "ConsumerParameter", "ConsumerResult", ConsumerResult.class, Collections.emptySet(), router);
}

}

private static class OpWithUnmappedExceptionProviderProxy extends ProviderOperationProxyTemplate<ProviderParameter, ProviderResult> {

public OpWithUnmappedExceptionProviderProxy() {
super("opWithUnmappedException", "ProviderParameter" , "ProviderResult", ProviderParameter.class);
}

@Override
protected ProviderResult invokeOperation(ProviderParameter parameter) {
ProviderTestException exceptionData = new ProviderTestException();
exceptionData.setExceptionField(1234);

throw new MappableProviderTestException(exceptionData);
}

}

private static class MonoToPolyMappingConsumerProxy extends ConsumerOperationProxyTemplate<ConsumerMonoToPolyType, ConsumerMonoToPolyType> {

public MonoToPolyMappingConsumerProxy(RequestRouter router) {
Expand Down

0 comments on commit 3368626

Please sign in to comment.