|
3 | 3 | */
|
4 | 4 | package io.airbyte.cdk.output
|
5 | 5 |
|
6 |
| -import io.airbyte.cdk.ConfigErrorException |
7 |
| -import io.airbyte.cdk.ConnectorErrorException |
8 |
| -import io.airbyte.cdk.SystemErrorException |
9 |
| -import io.airbyte.cdk.TransientErrorException |
10 |
| -import io.airbyte.cdk.util.ApmTraceUtils |
11 | 6 | import io.airbyte.protocol.models.v0.AirbyteErrorTraceMessage
|
12 |
| -import io.micronaut.context.annotation.DefaultImplementation |
13 |
| -import jakarta.inject.Singleton |
14 |
| -import org.apache.commons.lang3.exception.ExceptionUtils |
| 7 | +import io.micronaut.core.order.Ordered |
15 | 8 |
|
16 |
| -@Singleton |
17 |
| -@DefaultImplementation(DefaultExceptionClassifier::class) |
18 |
| -fun interface ExceptionClassifier { |
| 9 | +interface ExceptionClassifier : Ordered { |
19 | 10 |
|
20 | 11 | /** Classifies [e] into a [ConnectorError] if possible, null otherwise. */
|
21 | 12 | fun classify(e: Throwable): ConnectorError?
|
22 | 13 |
|
23 |
| - /** [SystemError] display message for [e] in case it can't be classified. */ |
24 |
| - fun fallbackDisplayMessage(e: Throwable): String? = e.message |
25 |
| - |
26 |
| - /** Maps [e] to a [AirbyteErrorTraceMessage] to be passed to the [OutputConsumer]. */ |
27 |
| - fun handle(e: Throwable): AirbyteErrorTraceMessage { |
28 |
| - ApmTraceUtils.addExceptionToTrace(e) |
29 |
| - val connectorError: ConnectorError = |
30 |
| - DefaultExceptionClassifier().classify(e) |
31 |
| - ?: classify(e) ?: SystemError(fallbackDisplayMessage(e) ?: e.message) |
32 |
| - val errorTraceMessage = |
33 |
| - AirbyteErrorTraceMessage() |
34 |
| - .withInternalMessage(e.toString()) |
35 |
| - .withStackTrace(ExceptionUtils.getStackTrace(e)) |
36 |
| - return when (connectorError) { |
37 |
| - is ConfigError -> |
38 |
| - errorTraceMessage |
39 |
| - .withFailureType(AirbyteErrorTraceMessage.FailureType.CONFIG_ERROR) |
40 |
| - .withMessage(connectorError.displayMessage) |
41 |
| - is TransientError -> |
42 |
| - errorTraceMessage |
43 |
| - .withFailureType(AirbyteErrorTraceMessage.FailureType.TRANSIENT_ERROR) |
44 |
| - .withMessage(connectorError.displayMessage) |
45 |
| - is SystemError -> |
46 |
| - errorTraceMessage |
47 |
| - .withFailureType(AirbyteErrorTraceMessage.FailureType.SYSTEM_ERROR) |
48 |
| - .withMessage(connectorError.displayMessage ?: e.message) |
49 |
| - } |
50 |
| - } |
| 14 | + /** Convenience val for [getOrder]. */ |
| 15 | + val orderValue: Int |
| 16 | + |
| 17 | + override fun getOrder(): Int = orderValue |
51 | 18 | }
|
52 | 19 |
|
53 | 20 | /** Each [ConnectorError] subtype corresponds to a [AirbyteErrorTraceMessage.FailureType]. */
|
@@ -75,29 +42,63 @@ data class TransientError(val displayMessage: String) : ConnectorError
|
75 | 42 | */
|
76 | 43 | data class SystemError(val displayMessage: String?) : ConnectorError
|
77 | 44 |
|
78 |
| -/** Default implementation of [ExceptionClassifier]. */ |
79 |
| -@Singleton |
80 |
| -class DefaultExceptionClassifier : ExceptionClassifier { |
| 45 | +/** Common Micronaut property prefix for all exception classifiers. */ |
| 46 | +const val EXCEPTION_CLASSIFIER_PREFIX = "airbyte.connector.exception-classifiers" |
| 47 | + |
| 48 | +/** Convenience interface for rules-based [ExceptionClassifier] implementations. */ |
| 49 | +interface RuleBasedExceptionClassifier<T : RuleBasedExceptionClassifier.Rule> : |
| 50 | + ExceptionClassifier { |
| 51 | + |
| 52 | + /** List of rules to match for. */ |
| 53 | + val rules: List<T> |
81 | 54 |
|
82 | 55 | override fun classify(e: Throwable): ConnectorError? {
|
83 |
| - return when (val connectorErrorException: ConnectorErrorException? = unwind(e)) { |
84 |
| - is ConfigErrorException -> ConfigError(connectorErrorException.message!!) |
85 |
| - is TransientErrorException -> TransientError(connectorErrorException.message!!) |
86 |
| - is SystemErrorException -> SystemError(connectorErrorException.message) |
87 |
| - null -> null |
| 56 | + for (rule in rules) { |
| 57 | + if (!rule.matches(e)) { |
| 58 | + continue |
| 59 | + } |
| 60 | + val message: String = rule.output ?: e.message ?: e.toString() |
| 61 | + val firstLine: String = if (rule.group == null) message else "${rule.group}: $message" |
| 62 | + val lines: List<String> = listOf(firstLine) + rule.referenceLinks |
| 63 | + val displayMessage: String = lines.joinToString(separator = "\n") |
| 64 | + return when (rule.error) { |
| 65 | + ErrorKind.CONFIG -> ConfigError(displayMessage) |
| 66 | + ErrorKind.TRANSIENT -> TransientError(displayMessage) |
| 67 | + ErrorKind.SYSTEM -> SystemError(displayMessage) |
| 68 | + } |
88 | 69 | }
|
| 70 | + return null |
89 | 71 | }
|
90 | 72 |
|
91 |
| - /** Recursively walks the causes of [e] and returns the last [ConnectorErrorException]. */ |
92 |
| - fun unwind(e: Throwable): ConnectorErrorException? { |
93 |
| - var connectorErrorException: ConnectorErrorException? = null |
94 |
| - var unwound: Throwable? = e |
95 |
| - while (unwound != null) { |
96 |
| - if (unwound is ConnectorErrorException) { |
97 |
| - connectorErrorException = unwound |
98 |
| - } |
99 |
| - unwound = unwound.cause |
100 |
| - } |
101 |
| - return connectorErrorException |
| 73 | + interface Rule : Ordered { |
| 74 | + |
| 75 | + /** Rule ordinal in the rule set. */ |
| 76 | + val ordinal: Int |
| 77 | + |
| 78 | + /** If the rule matches, the kind of [ConnectorError] to produce. */ |
| 79 | + val error: ErrorKind |
| 80 | + |
| 81 | + /** Optional display message prefix. */ |
| 82 | + val group: String? |
| 83 | + |
| 84 | + /** Optional display message. */ |
| 85 | + val output: String? |
| 86 | + |
| 87 | + /** Optional list of reference links to display. */ |
| 88 | + val referenceLinks: List<String> |
| 89 | + |
| 90 | + /** Rule predicate. */ |
| 91 | + fun matches(e: Throwable): Boolean |
| 92 | + |
| 93 | + override fun getOrder(): Int = ordinal |
| 94 | + |
| 95 | + /** Validates rule definition correctness. */ |
| 96 | + fun validate() |
| 97 | + } |
| 98 | + |
| 99 | + enum class ErrorKind { |
| 100 | + CONFIG, |
| 101 | + TRANSIENT, |
| 102 | + SYSTEM, |
102 | 103 | }
|
103 | 104 | }
|
0 commit comments