Skip to content

Commit d00c7bf

Browse files
authored
Merge pull request #757 from graphql-java-kickstart/477-dont-throw-undeclared-throwable
Remove usage of UndeclaredThrowableException
2 parents c3fb17d + 41737d3 commit d00c7bf

File tree

2 files changed

+33
-12
lines changed

2 files changed

+33
-12
lines changed

src/main/kotlin/graphql/kickstart/tools/resolver/MethodFieldResolver.kt

+4-12
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import graphql.schema.DataFetchingEnvironment
1515
import graphql.schema.GraphQLTypeUtil.isScalar
1616
import kotlinx.coroutines.future.future
1717
import org.slf4j.LoggerFactory
18+
import java.lang.reflect.InvocationTargetException
1819
import java.lang.reflect.Method
1920
import java.util.*
2021
import kotlin.coroutines.intrinsics.suspendCoroutineUninterceptedOrReturn
@@ -251,20 +252,11 @@ private suspend inline fun invokeSuspend(target: Any, resolverMethod: Method, ar
251252
}
252253
}
253254

254-
@Suppress("NOTHING_TO_INLINE")
255-
private inline fun invoke(method: Method, instance: Any, args: Array<Any?>): Any? {
255+
private fun invoke(method: Method, instance: Any, args: Array<Any?>): Any? {
256256
try {
257257
return method.invoke(instance, *args)
258-
} catch (invocationException: java.lang.reflect.InvocationTargetException) {
259-
val e = invocationException.cause
260-
if (e is RuntimeException) {
261-
throw e
262-
}
263-
if (e is Error) {
264-
throw e
265-
}
266-
267-
throw java.lang.reflect.UndeclaredThrowableException(e)
258+
} catch (e: InvocationTargetException) {
259+
throw e.cause ?: RuntimeException("Unknown error occurred while invoking resolver method")
268260
}
269261
}
270262

src/test/kotlin/graphql/kickstart/tools/MethodFieldResolverTest.kt

+29
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package graphql.kickstart.tools
22

3+
import graphql.ExceptionWhileDataFetching
34
import graphql.ExecutionInput
45
import graphql.GraphQL
56
import graphql.GraphQLContext
@@ -210,6 +211,34 @@ class MethodFieldResolverTest {
210211
assertEquals(result.getData(), mapOf("test" to 6))
211212
}
212213

214+
@Test
215+
fun `should unwrap and rethrow resolver exceptions`() {
216+
val schema = SchemaParser.newParser()
217+
.schemaString(
218+
"""
219+
type Query {
220+
test: String
221+
}
222+
""")
223+
.resolvers(object : GraphQLQueryResolver {
224+
fun test(): String = throw Exception("Whoops")
225+
})
226+
.build()
227+
.makeExecutableSchema()
228+
229+
val gql = GraphQL.newGraphQL(schema).build()
230+
val result = gql.execute(ExecutionInput.newExecutionInput().query(
231+
"""
232+
query {
233+
test
234+
}
235+
"""))
236+
237+
assertEquals(result.errors.size, 1)
238+
val exceptionWhileDataFetching = result.errors[0] as ExceptionWhileDataFetching
239+
assertEquals(exceptionWhileDataFetching.exception.message, "Whoops")
240+
}
241+
213242
/**
214243
* Custom Scalar Class type that doesn't work with Jackson serialization/deserialization
215244
*/

0 commit comments

Comments
 (0)