https://kotlinlang.org logo
#graphql-kotlin
Title
# graphql-kotlin
h

Hector Gonzalez

09/15/2020, 7:30 PM
Hi everyone! I am currently working on implementing custom error handling and logging using GraphQl and Kotlin on a federated service. Does anyone have a current example on how to do this? The examples I've found online seem to use deprecated classes.
d

Dariusz Kuc

09/16/2020, 4:52 AM
which library are you using? if you are using
graphql-kotlin
or
graphql-java
it should be the same as in non-federated service • for error handling you probably want to specify your own
DataFetcherExceptionHandler
• for logging (if it is something that you want to configure globally) you could use instrumentation
h

Hector Gonzalez

09/17/2020, 3:14 PM
Hi Dariusz and thanks for the response. We are using
graphql-kotlin
I've tried implementing `DataFetcherExceptionHandler`without success.
On the application properties I've set:
exception-handlers-enabled: true
I've also created a class to handle exceptions like this:
Copy code
@Component
class CustomGraphQlErrorHandler: DataFetcherExceptionHandler {
    private val logger = LoggerFactory.getLogger(CustomGraphQlErrorHandler::class.java)

    @ExceptionHandler
    override fun onException(handlerParameters: DataFetcherExceptionHandlerParameters?): DataFetcherExceptionHandlerResult {
        val exception= handlerParameters?.exception
        val sourceLocation= handlerParameters?.sourceLocation
        val path= handlerParameters?.path

        val error = ExceptionWhileDataFetching(path, exception, sourceLocation)

        return DataFetcherExceptionHandlerResult.newResult().error(error).build()
    }
}
d

Dariusz Kuc

09/17/2020, 3:29 PM
unsure what is the
exception-handlers-enabled
property as it is not something we use within the library
are you using
graphql-kotlin-spring-server
?
h

Hector Gonzalez

09/17/2020, 3:36 PM
yes, we are using
graphql-kotlin-spring-server
I'm not sure if the way I'm trying to trigger that code may be wrong though. What I'm currently doing is trying to retrieve a non-existing field in our schema. And that does triggers a validation error, but the code in the handler doesn't seem to execute.
If I throw an Exception in the resolver then the onHandle method executes just fine, but in cases where schema validation fails, it's not executed. Particularly we want to be able to change the message contents in errors like this:
Copy code
{
  "errors": [
    {
      "message": "Validation error of type FieldUndefined: Field 'categoryNames' in type 'Category' is undefined @ 'getCategoryById/categoryNames'",
      "locations": [
        {
          "line": 4,
          "column": 5
        }
      ]
    }
  ]
}
d

Dariusz Kuc

09/17/2020, 3:59 PM
query validation happens before any of the exception handlers
unsure if there is a way to customize those validation messages
we are relying on
graphql-java
logic there so you can take a look at their documentation but I don’t recall any way to customize validation exception handling
h

Hector Gonzalez

09/17/2020, 4:09 PM
I'll take a look at their documentation then. Thank you so much Dariusz!
76 Views