Spring Boot GraphQL Tutorial #8 - Exception Handling with ExceptionHandler

Опубликовано: 16 Сентябрь 2020
на канале: 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...


На этой странице сайта вы можете посмотреть видео онлайн Spring Boot GraphQL Tutorial #8 - Exception Handling with ExceptionHandler длительностью часов минут секунд в хорошем качестве, которое загрузил пользователь Philip Starritt 16 Сентябрь 2020, поделитесь ссылкой с друзьями и знакомыми, на youtube это видео уже посмотрели 17,571 раз и оно понравилось 102 зрителям. Приятного просмотра!