Skip to content

Commit

Permalink
Properly take @ClientExceptionMapper priority into account
Browse files Browse the repository at this point in the history
Fixes: #23396
  • Loading branch information
geoand committed Feb 3, 2022
1 parent 047180b commit ccf9d35
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.eclipse.microprofile.rest.client.ext.ResponseExceptionMapper;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationTarget;
import org.jboss.jandex.AnnotationValue;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.MethodInfo;
import org.jboss.jandex.Type;
Expand Down Expand Up @@ -82,26 +83,34 @@ Result generateResponseExceptionMapper(AnnotationInstance instance) {
for (Type i : targetMethod.parameters()) {
sigBuilder.append(i.name().toString());
}

int priority = Priorities.USER;
AnnotationValue priorityAnnotationValue = instance.value("priority");
if (priorityAnnotationValue != null) {
priority = priorityAnnotationValue.asInt();
}

ClassInfo restClientInterfaceClassInfo = targetMethod.declaringClass();
String generatedClassName = restClientInterfaceClassInfo.name().toString() + "_" + targetMethod.name() + "_"
+ "ResponseExceptionMapper" + "_" + HashUtil.sha1(sigBuilder.toString());
try (ClassCreator cc = ClassCreator.builder().classOutput(classOutput).className(generatedClassName)
.interfaces(ResponseExceptionMapper.class).build()) {
MethodCreator mc = cc.getMethodCreator("toThrowable", Throwable.class, Response.class);
ResultHandle resultHandle = mc.invokeStaticInterfaceMethod(
MethodCreator toThrowable = cc.getMethodCreator("toThrowable", Throwable.class, Response.class);
ResultHandle resultHandle = toThrowable.invokeStaticInterfaceMethod(
MethodDescriptor.ofMethod(
restClientInterfaceClassInfo.name().toString(),
targetMethod.name(),
targetMethod.returnType().name().toString(),
targetMethod.parameters().get(0).name().toString()),
mc.getMethodParam(0));
mc.returnValue(resultHandle);
}
toThrowable.getMethodParam(0));
toThrowable.returnValue(resultHandle);

int priority = Priorities.USER;
if (instance.value() != null) {
priority = instance.value().asInt();
if (priority != Priorities.USER) {
MethodCreator getPriority = cc.getMethodCreator("getPriority", int.class);
getPriority.returnValue(getPriority.load(priority));
}
}

return new Result(restClientInterfaceClassInfo.name().toString(), generatedClassName, priority);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public class RegisteredClientExceptionMapperTest {
.addAsResource(
new StringAsset(setUrlForClass(ClientNoProviders.class)
+ setUrlForClass(ClientWithRegisteredLowPriorityMapper.class)
+ setUrlForClass(ClientWithRegisteredHighPriorityMapper.class)),
+ setUrlForClass(ClientWithRegisteredHighPriorityMapper.class)
+ setUrlForClass(ClientWithRegisteredHighPriorityMapperAndSetPriority.class)),
"application.properties"));

@RestClient
Expand All @@ -44,6 +45,9 @@ public class RegisteredClientExceptionMapperTest {
@RestClient
ClientWithRegisteredHighPriorityMapper clientWithRegisteredHighPriorityMapper;

@RestClient
ClientWithRegisteredHighPriorityMapperAndSetPriority clientWithRegisteredHighPriorityMapperAndSetPriority;

@BeforeEach
void setUp() {
DummyException.executionCount.set(0);
Expand Down Expand Up @@ -77,8 +81,8 @@ void customExceptionMapperDotNotEngageWhenRegisteredExceptionMapperHasHigherPrio

@Test
void customExceptionMapperEngagesWhenRegisteredExceptionMapperHasHigherPriority() {
assertThrows(DummyException3.class, clientWithRegisteredHighPriorityMapper::get404);
assertThat(DummyException3.executionCount.get()).isEqualTo(1);
assertThrows(DummyException.class, clientWithRegisteredHighPriorityMapperAndSetPriority::get404);
assertThat(DummyException.executionCount.get()).isEqualTo(1);
}

@Path("/error")
Expand Down

0 comments on commit ccf9d35

Please sign in to comment.