How to handle exceptions in DgsReactiveCustomContextBuilderWithRequest #1853
Replies: 5 comments
-
Have you tried setting up your own custom exception handler for this? https://netflix.github.io/dgs/error-handling/#mapping-custom-exceptions |
Beta Was this translation helpful? Give feedback.
-
We do have a custom exception handler. Unfortunately, exceptions happening in the ContextBuilder are not caught by it. Instead it is handled here: And from there on it isn't a proper gql error response anymore. It would be nice, to either properly handle exceptions and format them or at least allow hooking into Edit:
I then wrapped It works. But it is way to complicated IMHO |
Beta Was this translation helpful? Give feedback.
-
Couple more comments: In addition, you can also wrap your data fetchers in custom logic in the same instrumentation class and return a custom DataFetcherResult: https://www.graphql-java.com/documentation/execution#returning-data-and-errors. I'm thinking one or a combination of the above methods should address your use case. Usually graphql errors are returned after the request starts actual execution, and because this is part of the request building, the framework currently doesn't handle it. Finally, we are in the process of moving to an implementation that integrates with spring-graphql, so that would have better support for Webflux features that we don't use internally, and hence don't maintain it as much. So you can try that out as well very soon. Hope that helps. |
Beta Was this translation helpful? Give feedback.
-
I will give Instrumentation a try. Thank you for your input! I am looking forward to the spring-graphql integration |
Beta Was this translation helpful? Give feedback.
-
For reference. class RequestErrorInterceptor : WebGraphQlInterceptor {
override fun intercept(
request: WebGraphQlRequest,
chain: WebGraphQlInterceptor.Chain
): Mono<WebGraphQlResponse> = chain.next(request).onErrorResume { throwable ->
val typedGraphQLError = // build it somehow
val executionResult = ExecutionResult.newExecutionResult()
.addError(typedGraphQLError)
.build()
val executionGraphQlResponse = DefaultExecutionGraphQlResponse(
request.toExecutionInput(),
executionResult
)
Mono.just(WebGraphQlResponse(executionGraphQlResponse))
}
} |
Beta Was this translation helpful? Give feedback.
-
Heyho,
we have a quite heavy Custom GraphQLContext. We have implemented a DgsReactiveCustomContextBuilderWithRequest which also does some validation.
How shall we handle possible exceptions?
Mono.empty()
, the custom context becomesnull
and code downstream, cannot handle a null custom context leading to NPEsMono.error()
with the exception, the error is not handled and directly sent to the user. No proper error responseAny help would be helpful.
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions