Skip to content

Error thrown from @GraphQlExceptionHandler method results in ClassCastException #1090

Closed
@Sam-Kruglov

Description

@Sam-Kruglov

If my handler throws an error, then DataFetcherHandlerMethod (line 239) returns Mono.error(..), and then AnnotatedControllerExceptionResolver (line 241) tries to convert it to Mono again, which gives ClassCastException

Object result = exceptionHandler.invoke(env, arguments);
Mono<List<GraphQLError>> errorsMono = methodReturnValueAdapter.adapt(result, exception);

Specifically, in my case I see I threw an error from my handler @GraphQlExceptionHandler fun handle(...): List<GraphQLError> {...} that ended up here:

doInvoke:129, InvocableHandlerMethodSupport (org.springframework.graphql.data.method)
validateAndInvoke:142, DataFetcherHandlerMethod (org.springframework.graphql.data.method.annotation.support)
invoke:125, DataFetcherHandlerMethod (org.springframework.graphql.data.method.annotation.support)
invokeExceptionHandler:239, AnnotatedControllerExceptionResolver (org.springframework.graphql.data.method.annotation.support)
resolveException:211, AnnotatedControllerExceptionResolver (org.springframework.graphql.data.method.annotation.support)

After that, the Mono.error with my exception is attempted to be cast to the return type of my handler here:

ReturnValueAdapter forCollection = (result, returnType, ex) ->
(result != null) ?
Mono.just((result instanceof List) ?
(List<GraphQLError>) result :
new ArrayList<>((Collection<GraphQLError>) result)) :
Mono.empty();

Line 446 evaluates to false because it is instance of Mono and it tried to cast it to Collection<GraphQLError> and then fails.

And finally, I don't even get to see my actual error that got thrown in my exception handler in the logs, all I can see are these:

42:57.187 WARN --- [xec-7] a.s.AnnotatedControllerExceptionResolver: Failure while handling exception with *handler signature*
java.lang.ClassCastException...
42:57.189 WARN --- [xec-7] a.s.AnnotatedControllerExceptionResolver: Failure while handling exception with *handler signature*
java.lang.ClassCastException... --- why print the same log AGAIN?
42:57.190 WARN  --- [xec-7] s.g.e.ExceptionResolversExceptionHandler: Failure while resolving *resolver signature*
jakarta.validation.ConstraintViolationException... ---- error that got thrown in the code and that I was trying to handle

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions