Exception handler support
Support for Springs @ExceptionHandler
annotation. By default graphql-java-servlet
when an exception occurs while processing a GraphQL request the error returned to the caller is a GraphQLError
with a simple message and InternalServerError
type. All details regarding the exception that actually occurred are lost.
This release introduces the property graphql.servlet.exception-handlers-enabled
, which is set to false
by default. That ensures that the default behavior stays the same. By switching this property to true
it will instead actually use the exception that was thrown to construct the GraphQLError
response, e.g.:
{
"data": null,
"errors": [
{
"message": "User 'username' cannot be found at the Identity Provider",
"type": "AccessDeniedException",
"path": null,
"extensions": null
}
]
}
The message
contains the message as represented by the exception and the type
contains the simple name of the exception that was thrown.
In addition you can now add methods to your Spring beans annotated with Springs @ExceptionHandler
. This way you can easily customize the errors you want to return depending on the exception that was thrown while processing a GraphQL request, e.g.:
@ExceptionHandler(Throwable.class)
GraphQLError handleException(Throwable e) {
return new ThrowableGraphQLError(e);
}
This example would actually result in the exact same response as given in the example response above, but it shows the idea behind it. You can return any type of custom GraphQLError
for this method, and you can have any number of methods annotated like this. It will select the method targeting the most concrete exception that was thrown.