Spring Boot GraphQL Tutorial #8 - Exception Handling with ExceptionHandler

Publicado el: 16 septiembre 2020
en el canal de: 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...


En esta página del sitio puede ver el video en línea Spring Boot GraphQL Tutorial #8 - Exception Handling with ExceptionHandler de Duración hora minuto segunda en buena calidad , que subió el usuario Philip Starritt 16 septiembre 2020, comparta el enlace con amigos y conocidos, en youtube este video ya ha sido visto 17,571 veces y le gustó 102 a los espectadores. Disfruta viendo!