Skip to content

Commit f0b3590

Browse files
tk26smyrick
authored andcommitted
[spring-server] fix: Exclude stacktraces for exceptions (#426)
1 parent b0bdf1f commit f0b3590

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

graphql-kotlin-spring-server/src/main/kotlin/com/expediagroup/graphql/spring/model/GraphQLResponse.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616

1717
package com.expediagroup.graphql.spring.model
1818

19+
import com.expediagroup.graphql.spring.exception.SimpleKotlinGraphQLError
1920
import com.fasterxml.jackson.annotation.JsonInclude
2021
import com.fasterxml.jackson.annotation.JsonInclude.Include
2122
import graphql.ExecutionResult
2223
import graphql.GraphQLError
24+
import java.lang.Exception
2325

2426
@JsonInclude(Include.NON_NULL)
2527
data class GraphQLResponse(
@@ -29,7 +31,15 @@ data class GraphQLResponse(
2931
)
3032

3133
fun ExecutionResult.toGraphQLResponse(): GraphQLResponse {
32-
val filteredErrors = if (errors?.isNotEmpty() == true) errors else null
34+
val filteredErrors = if (errors?.isNotEmpty() == true) {
35+
errors.map {
36+
when (it) {
37+
is SimpleKotlinGraphQLError -> it
38+
is Exception -> SimpleKotlinGraphQLError(exception = it, locations = it.locations, path = it.path, errorType = it.errorType)
39+
else -> it
40+
}
41+
}
42+
} else null
3343
val filteredExtensions = if (extensions?.isNotEmpty() == true) extensions else null
3444
return GraphQLResponse(getData(), filteredErrors, filteredExtensions)
3545
}

graphql-kotlin-spring-server/src/test/kotlin/com/expediagroup/graphql/spring/model/GraphQLResponseKtTest.kt

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@
1616

1717
package com.expediagroup.graphql.spring.model
1818

19+
import com.expediagroup.graphql.spring.exception.SimpleKotlinGraphQLError
1920
import graphql.ExecutionResult
21+
import graphql.execution.ExecutionPath
22+
import graphql.execution.NonNullableValueCoercedAsNullException
23+
import graphql.language.SourceLocation
24+
import graphql.language.VariableDefinition
25+
import graphql.schema.GraphQLTypeReference
2026
import io.mockk.every
2127
import io.mockk.mockk
2228
import org.junit.jupiter.api.Test
@@ -91,4 +97,57 @@ class GraphQLResponseKtTest {
9197
assertNotNull(extensions)
9298
assertEquals(expected = "bar", actual = extensions["foo"])
9399
}
100+
101+
@Test
102+
fun `SimpleKotlinGraphQLError is mapped as expected`() {
103+
val executionResult: ExecutionResult = mockk {
104+
every { getData<Any>() } returns mockk()
105+
every { errors } returns listOf(SimpleKotlinGraphQLError(
106+
Exception(),
107+
listOf(SourceLocation(1, 1)),
108+
ExecutionPath.rootPath().toList()
109+
))
110+
every { extensions } returns mapOf("foo" to "bar")
111+
}
112+
113+
val result = executionResult.toGraphQLResponse()
114+
115+
assertNotNull(result.data)
116+
val errors = result.errors
117+
assertNotNull(errors)
118+
assertEquals(1, errors.size)
119+
assert(errors.first() is SimpleKotlinGraphQLError)
120+
val extensions = result.extensions
121+
assertNotNull(extensions)
122+
assertEquals(expected = "bar", actual = extensions["foo"])
123+
}
124+
125+
@Test
126+
fun `error due to an Exception is mapped as expected`() {
127+
val executionResult: ExecutionResult = mockk {
128+
every { getData<Any>() } returns mockk()
129+
every { errors } returns listOf(NonNullableValueCoercedAsNullException(
130+
VariableDefinition(
131+
"name",
132+
mockk()
133+
),
134+
"name",
135+
GraphQLTypeReference(
136+
"name"
137+
)
138+
))
139+
every { extensions } returns mapOf("foo" to "bar")
140+
}
141+
142+
val result = executionResult.toGraphQLResponse()
143+
144+
assertNotNull(result.data)
145+
val errors = result.errors
146+
assertNotNull(errors)
147+
assertEquals(1, errors.size)
148+
assert(errors.first() is SimpleKotlinGraphQLError)
149+
val extensions = result.extensions
150+
assertNotNull(extensions)
151+
assertEquals(expected = "bar", actual = extensions["foo"])
152+
}
94153
}

0 commit comments

Comments
 (0)