Skip to content

Commit 9d0ed77

Browse files
author
Miguel Gonzalez Sanchez
committed
add gRPC exception advice support for factory beans
1 parent d70346b commit 9d0ed77

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

grpc-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/autoconfigure/OnMissingErrorHandlerCondition.java

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33
import org.lognet.springboot.grpc.recovery.GRpcExceptionHandler;
44
import org.lognet.springboot.grpc.recovery.GRpcServiceAdvice;
55
import org.lognet.springboot.grpc.recovery.HandlerMethod;
6+
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
7+
import org.springframework.beans.factory.config.BeanDefinition;
68
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
79
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
810
import org.springframework.context.annotation.ConditionContext;
911
import org.springframework.core.MethodIntrospector;
1012
import org.springframework.core.annotation.AnnotatedElementUtils;
1113
import org.springframework.core.type.AnnotatedTypeMetadata;
14+
import org.springframework.core.type.MethodMetadata;
1215
import org.springframework.util.ReflectionUtils;
1316

1417
import java.lang.reflect.Method;
@@ -24,26 +27,33 @@ public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeM
2427
.get("value");
2528

2629
ReflectionUtils.MethodFilter f = method -> AnnotatedElementUtils.hasAnnotation(method, GRpcExceptionHandler.class);
27-
for(String adviceBeanName:context.getBeanFactory().getBeanNamesForAnnotation(GRpcServiceAdvice.class)){
28-
final String beanClassName = context.getBeanFactory().getBeanDefinition(adviceBeanName)
29-
.getBeanClassName();
30-
30+
for (String adviceBeanName : context.getBeanFactory().getBeanNamesForAnnotation(GRpcServiceAdvice.class)) {
31+
final String beanClassName = getBeanClassName(context.getBeanFactory().getBeanDefinition(adviceBeanName));
3132
try {
3233
for (Method method : MethodIntrospector.selectMethods(Class.forName(beanClassName), f)) {
3334
final Optional<Class<? extends Throwable>> handledException = HandlerMethod.getHandledException(method, false);
34-
if(handledException.isPresent() && handledException.get().isAssignableFrom(exc)){
35+
if (handledException.isPresent() && handledException.get().isAssignableFrom(exc)) {
3536
return ConditionOutcome.noMatch(String.format("Found %s handler at %s.%s",
36-
handledException.get().getName(),
37-
beanClassName,
38-
method.getName()
39-
));
37+
handledException.get().getName(),
38+
beanClassName,
39+
method.getName()
40+
));
4041
}
4142
}
4243
} catch (ClassNotFoundException e) {
43-
throw new IllegalStateException(e);
44+
throw new IllegalStateException(e);
4445
}
45-
};
46+
}
4647

4748
return ConditionOutcome.match();
4849
}
50+
51+
private String getBeanClassName(BeanDefinition beanDefinition) {
52+
if (beanDefinition instanceof AnnotatedBeanDefinition) { // definition with @Bean Annotation cause this issue
53+
MethodMetadata factoryMethodMetadata = ((AnnotatedBeanDefinition) beanDefinition).getFactoryMethodMetadata();
54+
return factoryMethodMetadata.getReturnTypeName();
55+
} else {
56+
return beanDefinition.getBeanClassName();
57+
}
58+
}
4959
}

0 commit comments

Comments
 (0)