|
| 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