Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When an exception was thrown from a dataloader, it would be wrapped i… #1330

Merged
merged 2 commits into from Nov 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -24,6 +24,7 @@ import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.util.ClassUtils
import java.util.concurrent.CompletableFuture
import java.util.concurrent.CompletionException

/**
* Default DataFetcherExceptionHandler used by the framework, can be replaced with a custom implementation.
Expand All @@ -41,7 +42,7 @@ class DefaultDataFetcherExceptionHandler : DataFetcherExceptionHandler {
}

private fun doHandleException(handlerParameters: DataFetcherExceptionHandlerParameters): DataFetcherExceptionHandlerResult {
val exception = handlerParameters.exception
val exception = unwrapCompletionException(handlerParameters.exception)
logger.error(
"Exception while executing data fetcher for ${handlerParameters.path}: ${exception.message}",
exception
Expand All @@ -62,6 +63,10 @@ class DefaultDataFetcherExceptionHandler : DataFetcherExceptionHandler {
.build()
}

private fun unwrapCompletionException(e: Throwable): Throwable {
return if (e is CompletionException && e.cause != null) e.cause!! else e
}

companion object {

private val logger: Logger = LoggerFactory.getLogger(DefaultDataFetcherExceptionHandler::class.java)
Expand Down
Expand Up @@ -26,6 +26,7 @@ import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.springframework.security.access.AccessDeniedException
import java.util.concurrent.CompletionException

internal class DefaultDataFetcherExceptionHandlerTest {

Expand Down Expand Up @@ -126,4 +127,22 @@ internal class DefaultDataFetcherExceptionHandlerTest {
// We return null here because we don't want graphql-java to write classification field
assertThat(result.errors[0].errorType).isNull()
}

@Test
fun `CompletionException returns wrapped error code`() {
val completionException = CompletionException(
"com.netflix.graphql.dgs.exceptions.DgsEntityNotFoundException: Requested entity not found",
DgsEntityNotFoundException()
)
every { dataFetcherExceptionHandlerParameters.exception }.returns(completionException)

val result = DefaultDataFetcherExceptionHandler().handleException(dataFetcherExceptionHandlerParameters).get()
assertThat(result.errors.size).isEqualTo(1)

val extensions = result.errors[0].extensions
assertThat(extensions["errorType"]).isEqualTo("NOT_FOUND")

// We return null here because we don't want graphql-java to write classification field
assertThat(result.errors[0].errorType).isNull()
}
}