Handler failure from GraphQlExceptionHandler method

Closes gh-1090
This commit is contained in:
rstoyanchev
2025-01-29 16:41:32 +00:00
parent d2002bbe84
commit 474fbcafa1
2 changed files with 34 additions and 2 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -356,7 +356,16 @@ final class AnnotatedControllerExceptionResolver implements HandlerDataFetcherEx
return this.method;
}
@SuppressWarnings("unchecked")
Mono<List<GraphQLError>> adapt(@Nullable Object result, Throwable ex) {
if (result instanceof Mono<?> errorMono && this.adapter != ReturnValueAdapter.forMono) {
return (Mono<List<GraphQLError>>) errorMono.onErrorMap((ex2) -> {
if (logger.isWarnEnabled()) {
logger.warn("Failure in @GraphQlExceptionHandler " + this.method, ex2);
}
return ex; // fall back to original exception
});
}
return this.adapter.adapt(result, this.returnType, ex);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -111,6 +111,19 @@ public class AnnotatedControllerExceptionResolverTests {
StepVerifier.create(resolver.resolveException(ex, this.environment, controller)).verifyComplete();
}
@Test // gh-1090
void failureFromResolver() {
ExceptionThrowingController controller = new ExceptionThrowingController();
Exception ex = new IllegalArgumentException("Bad input");
AnnotatedControllerExceptionResolver resolver = exceptionResolver();
resolver.registerController(controller.getClass());
StepVerifier.create(resolver.resolveException(ex, this.environment, controller))
.expectErrorSatisfies(actual -> assertThat(actual).isSameAs(ex))
.verify();
}
@Test
void resolveWithControllerAdvice() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
@@ -304,4 +317,14 @@ public class AnnotatedControllerExceptionResolverTests {
}
private static class ExceptionThrowingController {
@GraphQlExceptionHandler
GraphQLError handle(IllegalArgumentException ex) {
throw new IllegalStateException("failure in exception handler");
}
}
}