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

feat(2132): unwrap invocation target exception #2133

Merged
merged 9 commits into from
Feb 25, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.slf4j.event.Level
import org.springframework.util.ClassUtils
import java.lang.reflect.InvocationTargetException
import java.util.concurrent.CompletableFuture
import java.util.concurrent.CompletionException

Expand All @@ -50,6 +51,7 @@ open class DefaultDataFetcherExceptionHandler : DataFetcherExceptionHandler {
isSpringSecurityAccessException(
exception,
) -> TypedGraphQLError.newPermissionDeniedBuilder()

else -> TypedGraphQLError.newInternalErrorBuilder()
}
builder
Expand Down Expand Up @@ -82,7 +84,12 @@ open class DefaultDataFetcherExceptionHandler : DataFetcherExceptionHandler {
)
}

private fun unwrapCompletionException(e: Throwable): Throwable = if (e is CompletionException && e.cause != null) e.cause!! else e
private fun unwrapCompletionException(e: Throwable): Throwable =
when (e) {
is CompletionException -> unwrapCompletionException(e.cause ?: e)
is InvocationTargetException -> unwrapCompletionException(e.targetException)
else -> e
}

protected val logger: Logger get() = DefaultDataFetcherExceptionHandler.logger

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ import io.mockk.spyk
import io.mockk.verify
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertAll
import org.slf4j.Logger
import org.slf4j.event.Level
import org.slf4j.spi.NOPLoggingEventBuilder
import org.springframework.security.access.AccessDeniedException
import java.lang.IllegalStateException
import java.lang.reflect.InvocationTargetException
import java.util.concurrent.CompletionException

class DefaultDataFetcherExceptionHandlerTest {
Expand Down Expand Up @@ -245,4 +246,23 @@ class DefaultDataFetcherExceptionHandlerTest {
verify { loggerMock.atLevel(Level.ERROR) }
confirmVerified(loggerMock)
}

@Test
fun `unwraps the invocation target exception`() {
val invocation = InvocationTargetException(IllegalStateException("I'm illegal!"), "Target invocation happened")

val params =
DataFetcherExceptionHandlerParameters
.newExceptionParameters()
.exception(invocation)
.dataFetchingEnvironment(environment)
.build()

val result = DefaultDataFetcherExceptionHandler().handleException(params).get()

assertAll(
{ assertThat(result.errors.size).isEqualTo(1) },
{ assertThat(result.errors[0].message).containsSubsequence("java.lang.IllegalStateException: I'm illegal!") },
)
}
}
Loading