Skip to content

Commit 6b7deb9

Browse files
fix(graphql): support specification error types
1 parent 7bf5574 commit 6b7deb9

2 files changed

Lines changed: 173 additions & 7 deletions

File tree

‎sentry-graphql-core/src/main/java/io/sentry/graphql/SentryGraphqlInstrumentation.java‎

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import io.sentry.SpanOptions;
2626
import io.sentry.SpanStatus;
2727
import io.sentry.TypeCheckHint;
28+
import io.sentry.util.ExceptionUtils;
2829
import io.sentry.util.StringUtils;
2930
import java.util.Arrays;
3031
import java.util.List;
@@ -123,8 +124,7 @@ public void instrumentExecutionResultComplete(
123124
final @NotNull List<GraphQLError> errors = result.getErrors();
124125
if (errors != null) {
125126
for (GraphQLError error : errors) {
126-
String errorType = getErrorType(error);
127-
if (!isIgnored(errorType)) {
127+
if (!isIgnored(error)) {
128128
exceptionReporter.captureThrowable(
129129
new RuntimeException(error.getMessage()),
130130
new ExceptionReporter.ExceptionDetails(
@@ -154,19 +154,35 @@ private boolean isIgnored(final @Nullable String errorType) {
154154
|| ignoredErrorTypes.contains(errorType);
155155
}
156156

157-
private @Nullable String getErrorType(final @Nullable GraphQLError error) {
157+
private boolean isIgnored(final @Nullable GraphQLError error) {
158158
if (error == null) {
159-
return null;
159+
return false;
160160
}
161161
final @Nullable ErrorClassification errorType = error.getErrorType();
162162
if (errorType != null) {
163-
return errorType.toString();
163+
if (isIgnored(errorType.toString())) {
164+
return true;
165+
}
166+
try {
167+
final @Nullable Object specification = errorType.toSpecification(error);
168+
if (specification instanceof String && isIgnored((String) specification)) {
169+
return true;
170+
}
171+
if (specification instanceof Map) {
172+
final @Nullable Object specificationType = ((Map<?, ?>) specification).get("type");
173+
return specificationType instanceof String && isIgnored((String) specificationType);
174+
}
175+
} catch (Throwable throwable) {
176+
ExceptionUtils.rethrowIfFatal(throwable);
177+
// Fall back to capturing the error if a custom classification cannot be serialized.
178+
}
179+
return false;
164180
}
165181
final @Nullable Map<String, Object> extensions = error.getExtensions();
166182
if (extensions != null) {
167-
return StringUtils.toString(extensions.get("errorType"));
183+
return isIgnored(StringUtils.toString(extensions.get("errorType")));
168184
}
169-
return null;
185+
return false;
170186
}
171187

172188
public void beginExecuteOperation(
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
package io.sentry.graphql
2+
3+
import graphql.ErrorClassification
4+
import graphql.ExecutionResultImpl
5+
import graphql.GraphQLContext
6+
import graphql.GraphQLError
7+
import graphql.GraphqlErrorException
8+
import graphql.execution.instrumentation.parameters.InstrumentationExecutionParameters
9+
import io.sentry.IScopes
10+
import kotlin.test.Test
11+
import org.mockito.kotlin.any
12+
import org.mockito.kotlin.mock
13+
import org.mockito.kotlin.never
14+
import org.mockito.kotlin.verify
15+
import org.mockito.kotlin.whenever
16+
17+
class SentryGraphqlInstrumentationTest {
18+
private val scopes = mock<IScopes>()
19+
private val exceptionReporter = mock<ExceptionReporter>()
20+
private val parameters = mock<InstrumentationExecutionParameters>()
21+
22+
init {
23+
whenever(parameters.graphQLContext)
24+
.thenReturn(
25+
GraphQLContext.newContext()
26+
.of(SentryGraphqlInstrumentation.SENTRY_SCOPES_CONTEXT_KEY, scopes)
27+
.build()
28+
)
29+
}
30+
31+
@Test
32+
fun `ignores error by classification string`() {
33+
captureError(
34+
error(CustomErrorClassification("SOME_ERROR", mapOf("type" to "OTHER_ERROR"))),
35+
ignoredErrorTypes = listOf("SOME_ERROR"),
36+
)
37+
38+
verify(exceptionReporter, never()).captureThrowable(any(), any(), any())
39+
}
40+
41+
@Test
42+
fun `ignores error by classification specification type`() {
43+
captureError(
44+
error(
45+
CustomErrorClassification(
46+
"graphql.validation.interpolation.ResourceBundleMessageInterpolator\$ValidationErrorType@1",
47+
mapOf("type" to "ExtendedValidationError"),
48+
)
49+
),
50+
ignoredErrorTypes = listOf("ExtendedValidationError"),
51+
)
52+
53+
verify(exceptionReporter, never()).captureThrowable(any(), any(), any())
54+
}
55+
56+
@Test
57+
fun `ignores error by string classification specification`() {
58+
captureError(
59+
error(CustomErrorClassification("SOME_ERROR", "SPECIFICATION_ERROR")),
60+
ignoredErrorTypes = listOf("SPECIFICATION_ERROR"),
61+
)
62+
63+
verify(exceptionReporter, never()).captureThrowable(any(), any(), any())
64+
}
65+
66+
@Test
67+
fun `captures error when classification specification has no type`() {
68+
captureError(error(CustomErrorClassification("SOME_ERROR", mapOf("constraint" to "@Size"))))
69+
70+
verify(exceptionReporter).captureThrowable(any(), any(), any())
71+
}
72+
73+
@Test
74+
fun `captures error when classification specification is null`() {
75+
captureError(error(CustomErrorClassification("SOME_ERROR", null)))
76+
77+
verify(exceptionReporter).captureThrowable(any(), any(), any())
78+
}
79+
80+
@Test
81+
fun `captures error when classification specification type is unexpected`() {
82+
captureError(error(CustomErrorClassification("SOME_ERROR", mapOf("type" to 42))))
83+
84+
verify(exceptionReporter).captureThrowable(any(), any(), any())
85+
}
86+
87+
@Test
88+
fun `captures error when classification specification throws`() {
89+
captureError(error(ThrowingErrorClassification))
90+
91+
verify(exceptionReporter).captureThrowable(any(), any(), any())
92+
}
93+
94+
@Test
95+
fun `ignores error by extensions when classification is null`() {
96+
val error = mock<GraphQLError>()
97+
whenever(error.errorType).thenReturn(null)
98+
whenever(error.extensions).thenReturn(mapOf("errorType" to "EXTENSION_ERROR"))
99+
100+
captureError(error, ignoredErrorTypes = listOf("EXTENSION_ERROR"))
101+
102+
verify(exceptionReporter, never()).captureThrowable(any(), any(), any())
103+
}
104+
105+
@Test
106+
fun `captures non-ignored error`() {
107+
captureError(
108+
error(CustomErrorClassification("SOME_ERROR", mapOf("type" to "SPECIFICATION_ERROR"))),
109+
ignoredErrorTypes = listOf("OTHER_ERROR"),
110+
)
111+
112+
verify(exceptionReporter).captureThrowable(any(), any(), any())
113+
}
114+
115+
private fun captureError(error: GraphQLError, ignoredErrorTypes: List<String> = emptyList()) {
116+
val instrumentation =
117+
SentryGraphqlInstrumentation(
118+
null,
119+
NoOpSubscriptionHandler.getInstance(),
120+
exceptionReporter,
121+
ignoredErrorTypes,
122+
"manual.test",
123+
)
124+
val result = ExecutionResultImpl.newExecutionResult().addError(error).build()
125+
126+
instrumentation.instrumentExecutionResultComplete(parameters, result, null)
127+
}
128+
129+
private fun error(errorClassification: ErrorClassification): GraphQLError =
130+
GraphqlErrorException.newErrorException()
131+
.message("exception message")
132+
.errorClassification(errorClassification)
133+
.build()
134+
135+
private class CustomErrorClassification(
136+
private val stringValue: String,
137+
private val specification: Any?,
138+
) : ErrorClassification {
139+
override fun toSpecification(error: GraphQLError): Any? = specification
140+
141+
override fun toString(): String = stringValue
142+
}
143+
144+
private object ThrowingErrorClassification : ErrorClassification {
145+
override fun toSpecification(error: GraphQLError): Any =
146+
throw AssertionError("failed to create specification")
147+
148+
override fun toString(): String = "SOME_ERROR"
149+
}
150+
}

0 commit comments

Comments
 (0)