Spring Boot GraphQL Tutorial #8 - Exception Handling with ExceptionHandler

Published: 16 September 2020
on channel: Philip Starritt
17,571
102

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...


On this page of the site you can watch the video online Spring Boot GraphQL Tutorial #8 - Exception Handling with ExceptionHandler with a duration of hours minute second in good quality, which was uploaded by the user Philip Starritt 16 September 2020, share the link with friends and acquaintances, this video has already been watched 17,571 times on youtube and it was liked by 102 viewers. Enjoy your viewing!