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

Commit 9ec74d7

Browse files
committed
Allow custom graphql exception handlers fix #493
1 parent cdd2720 commit 9ec74d7

File tree

5 files changed

+12
-16
lines changed

5 files changed

+12
-16
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
1717
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1818
#
19-
version=8.1.0-SNAPSHOT
19+
version=8.1.1-SNAPSHOT
2020
### Project Metadata
2121
PROJECT_GROUP=com.graphql-java-kickstart
2222
PROJECT_NAME=graphql-spring-boot

graphql-kickstart-spring-support/src/main/java/graphql/kickstart/spring/error/ErrorHandlerSupplier.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package graphql.kickstart.spring.error;
22

33
import graphql.kickstart.execution.error.GraphQLErrorHandler;
4-
import java.util.Objects;
54
import java.util.function.Supplier;
65

76
public class ErrorHandlerSupplier implements Supplier<GraphQLErrorHandler> {
@@ -22,7 +21,7 @@ public boolean isPresent() {
2221
}
2322

2423
public void setErrorHandler(GraphQLErrorHandler errorHandler) {
25-
this.errorHandler = Objects.requireNonNull(errorHandler);
24+
this.errorHandler = errorHandler;
2625
}
2726

2827
}

graphql-kickstart-spring-support/src/main/java/graphql/kickstart/spring/error/GraphQLErrorFromExceptionHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
@Getter
2323
class GraphQLErrorFromExceptionHandler extends DefaultGraphQLErrorHandler {
2424

25-
private List<GraphQLErrorFactory> factories;
25+
private final List<GraphQLErrorFactory> factories;
2626

2727
GraphQLErrorFromExceptionHandler(List<GraphQLErrorFactory> factories) {
2828
this.factories = factories;

graphql-spring-boot-autoconfigure/src/main/java/graphql/kickstart/spring/web/boot/GraphQLWebAutoConfiguration.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,13 @@ public class GraphQLWebAutoConfiguration {
104104
public static final String SUBSCRIPTION_EXECUTION_STRATEGY = "subscriptionExecutionStrategy";
105105

106106
private final GraphQLServletProperties graphQLServletProperties;
107+
private final ErrorHandlerSupplier errorHandlerSupplier = new ErrorHandlerSupplier(null);
107108

108109
@Bean
109110
public GraphQLErrorStartupListener graphQLErrorStartupListener(
110111
@Autowired(required = false) GraphQLErrorHandler errorHandler) {
111-
return new GraphQLErrorStartupListener(new ErrorHandlerSupplier(errorHandler),
112+
errorHandlerSupplier.setErrorHandler(errorHandler);
113+
return new GraphQLErrorStartupListener(errorHandlerSupplier,
112114
graphQLServletProperties.isExceptionHandlersEnabled());
113115
}
114116

@@ -257,13 +259,9 @@ public GraphQLInvoker graphQLInvoker(GraphQLBuilder graphQLBuilder,
257259
@ConditionalOnMissingBean
258260
public GraphQLObjectMapper graphQLObjectMapper(
259261
ObjectProvider<ObjectMapperProvider> objectMapperProviderObjectProvider,
260-
@Autowired(required = false) GraphQLServletObjectMapperConfigurer objectMapperConfigurer,
261-
@Autowired(required = false) GraphQLErrorHandler errorHandler) {
262+
@Autowired(required = false) GraphQLServletObjectMapperConfigurer objectMapperConfigurer) {
262263
GraphQLObjectMapper.Builder builder = newBuilder();
263-
264-
if (errorHandler != null) {
265-
builder.withGraphQLErrorHandler(new ErrorHandlerSupplier(errorHandler));
266-
}
264+
builder.withGraphQLErrorHandler(errorHandlerSupplier);
267265

268266
ObjectMapperProvider objectMapperProvider = objectMapperProviderObjectProvider.getIfAvailable();
269267

@@ -272,7 +270,6 @@ public GraphQLObjectMapper graphQLObjectMapper(
272270
} else if (objectMapperConfigurer != null) {
273271
builder.withObjectMapperConfigurer(objectMapperConfigurer);
274272
}
275-
log.info("Building GraphQLObjectMapper including errorHandler: {}", errorHandler);
276273
return builder.build();
277274
}
278275

graphql-spring-boot-autoconfigure/src/test/java/graphql/kickstart/spring/web/boot/GraphQLErrorHandlerTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import org.springframework.web.bind.annotation.ExceptionHandler;
1818
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
1919

20-
public class GraphQLErrorHandlerTest extends AbstractAutoConfigurationTest {
20+
class GraphQLErrorHandlerTest extends AbstractAutoConfigurationTest {
2121

2222
private GraphQL gql;
2323
private GraphQLObjectMapper objectMapper;
@@ -39,7 +39,7 @@ public void setUp() {
3939
}
4040

4141
@Test
42-
public void illegalArgumentExceptionShouldBeHandledConcretely() {
42+
void illegalArgumentExceptionShouldBeHandledConcretely() {
4343
TestUtils.assertGraphQLError(
4444
gql,
4545
"query { illegalArgumentException }",
@@ -49,7 +49,7 @@ public void illegalArgumentExceptionShouldBeHandledConcretely() {
4949
}
5050

5151
@Test
52-
public void illegalStateExceptionShouldBeHandledByCatchAll() {
52+
void illegalStateExceptionShouldBeHandledByCatchAll() {
5353
TestUtils.assertGraphQLError(gql, "query { illegalStateException }",
5454
new ThrowableGraphQLError(new IllegalStateException("Illegal state"), "Catch all handler"),
5555
objectMapper);
@@ -58,7 +58,7 @@ public void illegalStateExceptionShouldBeHandledByCatchAll() {
5858
@Configuration
5959
static class BaseConfiguration {
6060

61-
public class Query implements GraphQLQueryResolver {
61+
public static class Query implements GraphQLQueryResolver {
6262

6363
boolean illegalArgumentException() {
6464
throw new IllegalArgumentException("Illegal argument");

0 commit comments

Comments
 (0)