Skip to content

Commit

Permalink
Support nested exception hierarchies without an abstract base class (#25
Browse files Browse the repository at this point in the history
)
  • Loading branch information
lbourdages authored Nov 6, 2023
1 parent c14c4f7 commit 1921507
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ The plugin is available on Maven Central :
<dependency>
<groupId>com.coveo</groupId>
<artifactId>feign-error-decoder</artifactId>
<version>3.0.0</version>
<version>3.1.0</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.coveo</groupId>
<artifactId>feign-error-decoder</artifactId>
<version>3.0.0</version>
<version>3.1.0</version>
<packaging>jar</packaging>

<name>Feign Reflection Error Decoder</name>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/coveo/feign/ReflectionErrorDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ private void processDeclaredThrownExceptions(Class<?>[] thrownExceptionsClasses)
throws InstantiationException, IllegalAccessException, InvocationTargetException {
for (Class<?> clazz : thrownExceptionsClasses) {
if (baseExceptionClass.isAssignableFrom(clazz)) {
if (Modifier.isAbstract(clazz.getModifiers())) {
if (!classHierarchySupplier.getSubClasses(clazz, basePackage).isEmpty()) {
extractExceptionInfoFromSubClasses(classHierarchySupplier, clazz);
} else {
extractExceptionInfo((Class<? extends S>) clazz);
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/com/coveo/feign/ReflectionErrorDecoderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

import com.coveo.feign.ReflectionErrorDecoderTestClasses.AdditionalNotInterfacedRuntimeException;
import com.coveo.feign.ReflectionErrorDecoderTestClasses.AdditionalRuntimeException;
import com.coveo.feign.ReflectionErrorDecoderTestClasses.BaseNotAbstractException;
import com.coveo.feign.ReflectionErrorDecoderTestClasses.ChildOfBaseNotAbstractException;
import com.coveo.feign.ReflectionErrorDecoderTestClasses.ConcreteServiceException;
import com.coveo.feign.ReflectionErrorDecoderTestClasses.ConcreteSubServiceException;
import com.coveo.feign.ReflectionErrorDecoderTestClasses.ConcreteSubServiceExceptionWithoutInterface;
Expand All @@ -32,9 +34,11 @@
import com.coveo.feign.ReflectionErrorDecoderTestClasses.ExceptionWithStringConstructorException;
import com.coveo.feign.ReflectionErrorDecoderTestClasses.ExceptionWithThrowableConstructorException;
import com.coveo.feign.ReflectionErrorDecoderTestClasses.ExceptionWithTwoStringsConstructorException;
import com.coveo.feign.ReflectionErrorDecoderTestClasses.GrandChildOfBaseNotAbstractException;
import com.coveo.feign.ReflectionErrorDecoderTestClasses.MultipleConstructorsException;
import com.coveo.feign.ReflectionErrorDecoderTestClasses.MultipleConstructorsWithOnlyThrowableArgumentsException;
import com.coveo.feign.ReflectionErrorDecoderTestClasses.TestApiClassWithDuplicateErrorCodeException;
import com.coveo.feign.ReflectionErrorDecoderTestClasses.TestApiClassWithInheritedButNotAbstractExceptions;
import com.coveo.feign.ReflectionErrorDecoderTestClasses.TestApiClassWithInheritedExceptions;
import com.coveo.feign.ReflectionErrorDecoderTestClasses.TestApiClassWithNoErrorCodeServiceException;
import com.coveo.feign.ReflectionErrorDecoderTestClasses.TestApiClassWithPlainExceptions;
Expand Down Expand Up @@ -139,6 +143,19 @@ public void testWithInheritedExceptions() throws Exception {
ConcreteServiceException.ERROR_CODE, ConcreteSubServiceException.ERROR_CODE);
}

@Test
public void testWithNotAbstractInheritedExceptions() throws Exception {
Map<String, ThrownExceptionDetails<ServiceException>> exceptionsThrown =
getExceptionsThrownMapFromErrorDecoder(
TestApiClassWithInheritedButNotAbstractExceptions.class);

assertThat(exceptionsThrown.keySet())
.containsExactly(
BaseNotAbstractException.ERROR_CODE,
ChildOfBaseNotAbstractException.ERROR_CODE,
GrandChildOfBaseNotAbstractException.ERROR_CODE);
}

@Test
public void testWithUnannotatedMethod() throws Exception {
Map<String, ThrownExceptionDetails<ServiceException>> exceptionsThrown =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ public interface TestApiClassWithInheritedExceptions {
void methodWithAbstractException() throws AbstractServiceException;
}

public interface TestApiClassWithInheritedButNotAbstractExceptions {
@RequestLine("")
void methodWithInheritedNotAbstractException() throws BaseNotAbstractException;
}

public interface TestApiClassWithDuplicateErrorCodeException {
@RequestLine("")
void methodWithDuplicateErrorCodeException()
Expand Down Expand Up @@ -349,4 +354,40 @@ public MultipleConstructorsWithOnlyThrowableArgumentsException(Throwable cause)
super(ERROR_CODE, "", cause);
}
}

public static class BaseNotAbstractException extends ServiceException {
private static final long serialVersionUID = 1L;
public static final String ERROR_CODE = "ABSTRACT CONSIDERED HARMFUL";

public BaseNotAbstractException(Throwable e) {
super(ERROR_CODE, e);
}

protected BaseNotAbstractException(String errorCode, String message) {
super(errorCode, message);
}
}

public static class ChildOfBaseNotAbstractException extends BaseNotAbstractException {
private static final long serialVersionUID = 1L;
public static final String ERROR_CODE = "THIS IS PERFECTLY ACCEPTABLE";

public ChildOfBaseNotAbstractException(String message) {
super(ERROR_CODE, message);
}

protected ChildOfBaseNotAbstractException(String errorCode, String message) {
super(errorCode, message);
}
}

public static class GrandChildOfBaseNotAbstractException extends ChildOfBaseNotAbstractException {
private static final long serialVersionUID = 1L;
public static final String ERROR_CODE =
"THIS IS ARGUABLY A BAD PRACTICE BUT IT SHOULD STILL WORK";

public GrandChildOfBaseNotAbstractException(String message) {
super(ERROR_CODE, message);
}
}
}

0 comments on commit 1921507

Please sign in to comment.