Inject prepared GraphqlErrorBuilder in error handling methods
Prior to this commit, error handling methods would support various arguments, including the exception being handled. Our reference documentation would advise to create a new instance of a `GraphQLError` using `GraphQLError.newError()`. This does not initialize the location and path information of the current error. This commit allows error handling methods to get injected with a `GraphQlErrorBuilder<?>` argument that is initialized with the current `DataFetchingEnvironment` (thus filling the location and path parts). Fixes gh-1200
This commit is contained in:
@@ -886,37 +886,14 @@ Use `@GraphQlExceptionHandler` methods to handle exceptions from data fetching w
|
||||
flexible xref:controllers.adoc#controllers.exception-handler.signature[method signature]. When declared in a
|
||||
controller, exception handler methods apply to exceptions from the same controller:
|
||||
|
||||
[source,java,indent=0,subs="verbatim,quotes"]
|
||||
----
|
||||
@Controller
|
||||
public class BookController {
|
||||
|
||||
@QueryMapping
|
||||
public Book bookById(@Argument Long id) {
|
||||
// ...
|
||||
}
|
||||
|
||||
@GraphQlExceptionHandler
|
||||
public GraphQLError handle(BindException ex) {
|
||||
return GraphQLError.newError().errorType(ErrorType.BAD_REQUEST).message("...").build();
|
||||
}
|
||||
}
|
||||
----
|
||||
include-code::BookController[]
|
||||
|
||||
When declared in an `@ControllerAdvice`, exception handler methods apply across controllers:
|
||||
|
||||
[source,java,indent=0,subs="verbatim,quotes"]
|
||||
----
|
||||
@ControllerAdvice
|
||||
public class GlobalExceptionHandler {
|
||||
include-code::GlobalExceptionHandler[]
|
||||
|
||||
@GraphQlExceptionHandler
|
||||
public GraphQLError handle(BindException ex) {
|
||||
return GraphQLError.newError().errorType(ErrorType.BAD_REQUEST).message("...").build();
|
||||
}
|
||||
|
||||
}
|
||||
----
|
||||
As shown in the examples above, you should build errors by injecting `GraphQlErrorBuilder` in the method signature
|
||||
because it's been prepared with the current `DataFetchingEnvironment`.
|
||||
|
||||
Exception handling via `@GraphQlExceptionHandler` methods is applied automatically to
|
||||
controller invocations. To handle exceptions from other `graphql.schema.DataFetcher`
|
||||
|
||||
Reference in New Issue
Block a user