Skip to content

Commit

Permalink
Merge pull request #757 from graphql-java-kickstart/477-dont-throw-un…
Browse files Browse the repository at this point in the history
…declared-throwable

Remove usage of UndeclaredThrowableException
  • Loading branch information
oryan-block authored Aug 2, 2023
2 parents c3fb17d + 41737d3 commit d00c7bf
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import graphql.schema.DataFetchingEnvironment
import graphql.schema.GraphQLTypeUtil.isScalar
import kotlinx.coroutines.future.future
import org.slf4j.LoggerFactory
import java.lang.reflect.InvocationTargetException
import java.lang.reflect.Method
import java.util.*
import kotlin.coroutines.intrinsics.suspendCoroutineUninterceptedOrReturn
Expand Down Expand Up @@ -251,20 +252,11 @@ private suspend inline fun invokeSuspend(target: Any, resolverMethod: Method, ar
}
}

@Suppress("NOTHING_TO_INLINE")
private inline fun invoke(method: Method, instance: Any, args: Array<Any?>): Any? {
private fun invoke(method: Method, instance: Any, args: Array<Any?>): Any? {
try {
return method.invoke(instance, *args)
} catch (invocationException: java.lang.reflect.InvocationTargetException) {
val e = invocationException.cause
if (e is RuntimeException) {
throw e
}
if (e is Error) {
throw e
}

throw java.lang.reflect.UndeclaredThrowableException(e)
} catch (e: InvocationTargetException) {
throw e.cause ?: RuntimeException("Unknown error occurred while invoking resolver method")
}
}

Expand Down
29 changes: 29 additions & 0 deletions src/test/kotlin/graphql/kickstart/tools/MethodFieldResolverTest.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package graphql.kickstart.tools

import graphql.ExceptionWhileDataFetching
import graphql.ExecutionInput
import graphql.GraphQL
import graphql.GraphQLContext
Expand Down Expand Up @@ -210,6 +211,34 @@ class MethodFieldResolverTest {
assertEquals(result.getData(), mapOf("test" to 6))
}

@Test
fun `should unwrap and rethrow resolver exceptions`() {
val schema = SchemaParser.newParser()
.schemaString(
"""
type Query {
test: String
}
""")
.resolvers(object : GraphQLQueryResolver {
fun test(): String = throw Exception("Whoops")
})
.build()
.makeExecutableSchema()

val gql = GraphQL.newGraphQL(schema).build()
val result = gql.execute(ExecutionInput.newExecutionInput().query(
"""
query {
test
}
"""))

assertEquals(result.errors.size, 1)
val exceptionWhileDataFetching = result.errors[0] as ExceptionWhileDataFetching
assertEquals(exceptionWhileDataFetching.exception.message, "Whoops")
}

/**
* Custom Scalar Class type that doesn't work with Jackson serialization/deserialization
*/
Expand Down

0 comments on commit d00c7bf

Please sign in to comment.