Skip to content

Commit 474fbca

Browse files
committed
Handler failure from GraphQlExceptionHandler method
Closes gh-1090
1 parent d2002bb commit 474fbca

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

spring-graphql/src/main/java/org/springframework/graphql/data/method/annotation/support/AnnotatedControllerExceptionResolver.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -356,7 +356,16 @@ Method getMethod() {
356356
return this.method;
357357
}
358358

359+
@SuppressWarnings("unchecked")
359360
Mono<List<GraphQLError>> adapt(@Nullable Object result, Throwable ex) {
361+
if (result instanceof Mono<?> errorMono && this.adapter != ReturnValueAdapter.forMono) {
362+
return (Mono<List<GraphQLError>>) errorMono.onErrorMap((ex2) -> {
363+
if (logger.isWarnEnabled()) {
364+
logger.warn("Failure in @GraphQlExceptionHandler " + this.method, ex2);
365+
}
366+
return ex; // fall back to original exception
367+
});
368+
}
360369
return this.adapter.adapt(result, this.returnType, ex);
361370
}
362371

spring-graphql/src/test/java/org/springframework/graphql/data/method/annotation/support/AnnotatedControllerExceptionResolverTests.java

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -111,6 +111,19 @@ void leaveUnresolvedViaNullReturnValue() {
111111
StepVerifier.create(resolver.resolveException(ex, this.environment, controller)).verifyComplete();
112112
}
113113

114+
@Test // gh-1090
115+
void failureFromResolver() {
116+
ExceptionThrowingController controller = new ExceptionThrowingController();
117+
118+
Exception ex = new IllegalArgumentException("Bad input");
119+
AnnotatedControllerExceptionResolver resolver = exceptionResolver();
120+
resolver.registerController(controller.getClass());
121+
122+
StepVerifier.create(resolver.resolveException(ex, this.environment, controller))
123+
.expectErrorSatisfies(actual -> assertThat(actual).isSameAs(ex))
124+
.verify();
125+
}
126+
114127
@Test
115128
void resolveWithControllerAdvice() {
116129
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
@@ -304,4 +317,14 @@ public String handle(IllegalArgumentException ex) {
304317

305318
}
306319

320+
321+
private static class ExceptionThrowingController {
322+
323+
@GraphQlExceptionHandler
324+
GraphQLError handle(IllegalArgumentException ex) {
325+
throw new IllegalStateException("failure in exception handler");
326+
}
327+
328+
}
329+
307330
}

0 commit comments

Comments
 (0)