Skip to content

Commit

Permalink
Merge pull request #2133 from l3r8yJ/2132
Browse files Browse the repository at this point in the history
feat(2132): unwrap invocation target exception
  • Loading branch information
paulbakker authored Feb 25, 2025
2 parents 01710bb + 02a26ff commit 8097041
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
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!") },
)
}
}

0 comments on commit 8097041

Please sign in to comment.