Skip to content

Remove usage of UndeclaredThrowableException #757

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

Merged
merged 4 commits into from
Aug 2, 2023
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 @@ -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