Skip to content
This repository was archived by the owner on Dec 19, 2023. It is now read-only.

Commit fd01277

Browse files
committed
Only include exception handlers returning GraphQLError
1 parent 1acdac9 commit fd01277

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

graphql-spring-boot-autoconfigure/src/main/java/com/oembedler/moon/graphql/boot/error/GraphQLErrorHandlerFactory.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.oembedler.moon.graphql.boot.error;
22

3+
import graphql.GraphQLError;
34
import graphql.servlet.DefaultGraphQLErrorHandler;
45
import graphql.servlet.GraphQLErrorHandler;
56
import lombok.extern.slf4j.Slf4j;
@@ -9,6 +10,7 @@
910
import org.springframework.context.ConfigurableApplicationContext;
1011
import org.springframework.web.bind.annotation.ExceptionHandler;
1112

13+
import java.lang.reflect.Method;
1214
import java.util.Arrays;
1315
import java.util.Collections;
1416
import java.util.List;
@@ -21,6 +23,7 @@ public class GraphQLErrorHandlerFactory {
2123
public GraphQLErrorHandler create(ConfigurableApplicationContext applicationContext, boolean exceptionHandlersEnabled) {
2224
ConfigurableListableBeanFactory beanFactory = applicationContext.getBeanFactory();
2325
List<GraphQLErrorFactory> factories = Arrays.stream(beanFactory.getBeanDefinitionNames())
26+
.filter(applicationContext::containsBean)
2427
.map(beanFactory::getBeanDefinition)
2528
.map(BeanDefinition::getBeanClassName)
2629
.filter(Objects::nonNull)
@@ -38,9 +41,8 @@ public GraphQLErrorHandler create(ConfigurableApplicationContext applicationCont
3841
private List<GraphQLErrorFactory> scanForExceptionHandlers(ApplicationContext context, ConfigurableListableBeanFactory beanFactory, String className) {
3942
try {
4043
Class<?> objClz = beanFactory.getBeanClassLoader().loadClass(className);
41-
// todo: need to handle proxies?
4244
return Arrays.stream(objClz.getDeclaredMethods())
43-
.filter(method -> method.isAnnotationPresent(ExceptionHandler.class))
45+
.filter(this::isGraphQLExceptionHandlerMethod)
4446
.map(method -> GraphQLErrorFactory.withReflection(context.getBean(className), method))
4547
.collect(Collectors.toList());
4648
} catch (ClassNotFoundException e) {
@@ -49,4 +51,8 @@ private List<GraphQLErrorFactory> scanForExceptionHandlers(ApplicationContext co
4951
}
5052
}
5153

54+
private boolean isGraphQLExceptionHandlerMethod(Method method) {
55+
return method.isAnnotationPresent(ExceptionHandler.class) && GraphQLError.class.isAssignableFrom(method.getReturnType());
56+
}
57+
5258
}

graphql-spring-boot-autoconfigure/src/test/java/com/oembedler/moon/graphql/boot/GraphQLErrorHandlerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ boolean illegalStateException() {
7272
}
7373

7474
@ExceptionHandler(IllegalArgumentException.class)
75-
GraphQLError handle(IllegalArgumentException e) {
75+
ThrowableGraphQLError handle(IllegalArgumentException e) {
7676
return new ThrowableGraphQLError(e, "Illegal argument");
7777
}
7878

0 commit comments

Comments
 (0)