Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error thrown in custom handler fails #1090

Open
Sam-Kruglov opened this issue Nov 21, 2024 · 2 comments
Open

Error thrown in custom handler fails #1090

Sam-Kruglov opened this issue Nov 21, 2024 · 2 comments
Labels
status: feedback-provided Feedback has been provided status: waiting-for-triage An issue we've not yet triaged

Comments

@Sam-Kruglov
Copy link

Sam-Kruglov commented Nov 21, 2024

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
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Nov 21, 2024
@bclozel
Copy link
Member

bclozel commented Nov 22, 2024

Thanks for the analysis but you are one step ahead of us. Can you show a minimal error handler code snippet that would trigger this so we can have a look?

@bclozel bclozel added the status: waiting-for-feedback We need additional information before we can continue label Nov 22, 2024
@Sam-Kruglov
Copy link
Author

Sam-Kruglov commented Nov 23, 2024

Just throw any error. I believe it doesn't matter how you get to the exception handler, only matters that the handler itself throws an error.

@GraphQlExceptionHandler
fun handle(ex: ConstraintViolationException, env: DataFetchingEnvironment): List<GraphQLError> {
   throw IllegalStateException("oops")
}

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Nov 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: feedback-provided Feedback has been provided status: waiting-for-triage An issue we've not yet triaged
Projects
None yet
Development

No branches or pull requests

3 participants