Skip to content

Commit 03a881e

Browse files
dariuszkucsmyrick
authored andcommitted
Update default exception handler to use SimpleKotlinGraphQLError (#369)
By default, our default exception handler was wrapping all the runtime exceptions in `graphql-java` `ExceptionWhileDataFetching` GraphQLError. Since this default GraphQL error exposes `exception` field it was also getting serialized in the response. While this is was still valid GraphQL response, per [spec](https://graphql.github.io/graphql-spec/June2018/#sec-Errors) `exception` field is not part of the defined error fields and instead should be included in the error `extensions` entry field instead (if at all). Note: the above serialization issue could also be solved by using `graphql-java` `ExecutionResult#toSpecification` method to generate final results but that would also imply that we would need to return `Map` instead of a `GraphQLResponse`.
1 parent 50f3792 commit 03a881e

File tree

3 files changed

+6
-5
lines changed

3 files changed

+6
-5
lines changed

examples/spring/src/main/kotlin/com/expediagroup/graphql/sample/exceptions/CustomDataFetcherExceptionHandler.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.expediagroup.graphql.sample.exceptions
1818

19+
import com.expediagroup.graphql.spring.exception.SimpleKotlinGraphQLError
1920
import com.fasterxml.jackson.annotation.JsonIgnoreProperties
2021
import graphql.ErrorType
2122
import graphql.ErrorType.ValidationError
@@ -38,7 +39,7 @@ class CustomDataFetcherExceptionHandler : DataFetcherExceptionHandler {
3839

3940
val error: GraphQLError = when(exception) {
4041
is ValidationException -> ValidationDataFetchingGraphQLError(exception.constraintErrors, path, exception, sourceLocation)
41-
else -> ExceptionWhileDataFetching(path, exception, sourceLocation)
42+
else -> SimpleKotlinGraphQLError(exception = exception, locations = listOf(sourceLocation), path = path.toList())
4243
}
4344
log.warn(error.message, exception)
4445
return DataFetcherExceptionHandlerResult.newResult().error(error).build()

examples/spring/src/main/kotlin/com/expediagroup/graphql/sample/query/DataAndErrors.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
package com.expediagroup.graphql.sample.query
1818

19+
import com.expediagroup.graphql.spring.exception.SimpleKotlinGraphQLError
1920
import com.expediagroup.graphql.spring.operations.Query
20-
import graphql.ExceptionWhileDataFetching
2121
import graphql.execution.DataFetcherResult
2222
import graphql.execution.ExecutionPath
2323
import graphql.language.SourceLocation
@@ -28,15 +28,15 @@ import java.util.concurrent.CompletableFuture
2828
class DataAndErrors : Query {
2929

3030
fun returnDataAndErrors(): DataFetcherResult<String> {
31-
val error = ExceptionWhileDataFetching(ExecutionPath.rootPath(), RuntimeException(), SourceLocation(1, 1))
31+
val error = SimpleKotlinGraphQLError(RuntimeException(), listOf(SourceLocation(1, 1)), ExecutionPath.rootPath().toList())
3232
return DataFetcherResult.newResult<String>()
3333
.data("Hello from data fetcher")
3434
.error(error)
3535
.build()
3636
}
3737

3838
fun completableFutureDataAndErrors(): CompletableFuture<DataFetcherResult<String>> {
39-
val error = ExceptionWhileDataFetching(ExecutionPath.rootPath(), RuntimeException(), SourceLocation(1, 1))
39+
val error = SimpleKotlinGraphQLError(RuntimeException(), listOf(SourceLocation(1, 1)), ExecutionPath.rootPath().toList())
4040
val dataFetcherResult = DataFetcherResult.newResult<String>()
4141
.data("Hello from data fetcher")
4242
.error(error)

graphql-kotlin-spring-server/src/main/kotlin/com/expediagroup/graphql/spring/exception/KotlinDataFetcherExceptionHandler.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class KotlinDataFetcherExceptionHandler : DataFetcherExceptionHandler {
3535
val sourceLocation = handlerParameters.sourceLocation
3636
val path = handlerParameters.path
3737

38-
val error: GraphQLError = ExceptionWhileDataFetching(path, exception, sourceLocation)
38+
val error: GraphQLError = SimpleKotlinGraphQLError(exception = exception, locations = listOf(sourceLocation), path = path.toList())
3939
logger.warn(error.message, exception)
4040
return DataFetcherExceptionHandlerResult.newResult(error).build()
4141
}

0 commit comments

Comments
 (0)