Spring boot graphql supports Spring web's @ExceptionHandler annotations.
To enable this we first need to set the graphql property: graphql.servlet.exception-handlers-enabled: true. We can then proceed to create a component with @ExceptionHandler methods, that will transform an exception into a GraphQLError implementation. This is the hook point where you can place your exception handling logic. In this example, I transform the exceptions into a simple ThrowableGraphQLError, but many are available in the library or you have the option to customize your own.
With this approach I recommend you create an "catch all" exception handler. This will ensure any spring, feign, jooq, hibernate etc runtime exception messages are not propagated to the client, but instead are shown a internal server error message. To sum up, if its not defined handled exception type, then show internal server error. Lets not leak info.
By default, graphql will create a DefaultGraphQLErrorHandler bean. This bean will be invoked with a list of all the graphql exceptions/errors that occurred during the query. The bean will then try to find a matching ExceptionHandler for the error's exception. If it finds a matching handler, it will then swap its wrapped error (containing the real exception) with your ErrorHandlers response object.
See you in the next video. Spring Boot GraphQL Exception Handling with a custom GraphQLErrorHandler implementation.
Cheers!
Philip
GraphQL Spring Boot: https://github.com/graphql-java-kicks...
Spring Boot ExceptionHandler: https://docs.spring.io/spring-framewo...
Thumbnail image from: https://proandroiddev.com/android-err...
Auf dieser Seite können Sie das Online-Video Spring Boot GraphQL Tutorial #8 - Exception Handling with ExceptionHandler mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer Philip Starritt 16 September 2020 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 17,571 Mal angesehen und es wurde von 102 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!