-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bulk-cdk: add more exception classifier implementations, add extra ch…
…ecks (#44824)
- Loading branch information
Showing
20 changed files
with
690 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
...te-cdk/bulk/core/base/src/main/kotlin/io/airbyte/cdk/output/DefaultExceptionClassifier.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright (c) 2024 Airbyte, Inc., all rights reserved. | ||
*/ | ||
|
||
package io.airbyte.cdk.output | ||
|
||
import io.airbyte.cdk.ConfigErrorException | ||
import io.airbyte.cdk.ConnectorErrorException | ||
import io.airbyte.cdk.SystemErrorException | ||
import io.airbyte.cdk.TransientErrorException | ||
import io.micronaut.context.annotation.ConfigurationProperties | ||
import io.micronaut.context.annotation.Value | ||
import jakarta.inject.Singleton | ||
|
||
const val DEFAULT_CLASSIFIER_PREFIX = "${EXCEPTION_CLASSIFIER_PREFIX}.default" | ||
|
||
/** Default implementation of [ExceptionClassifier]. */ | ||
@Singleton | ||
@ConfigurationProperties(DEFAULT_CLASSIFIER_PREFIX) | ||
class DefaultExceptionClassifier( | ||
@Value("\${$DEFAULT_CLASSIFIER_PREFIX.order:1}") override val orderValue: Int | ||
) : ExceptionClassifier { | ||
|
||
override fun classify(e: Throwable): ConnectorError? { | ||
return when (val connectorErrorException: ConnectorErrorException? = unwind(e)) { | ||
is ConfigErrorException -> ConfigError(connectorErrorException.message!!) | ||
is TransientErrorException -> TransientError(connectorErrorException.message!!) | ||
is SystemErrorException -> SystemError(connectorErrorException.message) | ||
null -> null | ||
} | ||
} | ||
|
||
/** Recursively walks the causes of [e] and returns the last [ConnectorErrorException]. */ | ||
fun unwind(e: Throwable): ConnectorErrorException? { | ||
var connectorErrorException: ConnectorErrorException? = null | ||
var unwound: Throwable? = e | ||
while (unwound != null) { | ||
if (unwound is ConnectorErrorException) { | ||
connectorErrorException = unwound | ||
} | ||
unwound = unwound.cause | ||
} | ||
return connectorErrorException | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
airbyte-cdk/bulk/core/base/src/main/kotlin/io/airbyte/cdk/output/ExceptionHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright (c) 2024 Airbyte, Inc., all rights reserved. | ||
*/ | ||
|
||
package io.airbyte.cdk.output | ||
|
||
import io.airbyte.cdk.util.ApmTraceUtils | ||
import io.airbyte.protocol.models.v0.AirbyteErrorTraceMessage | ||
import jakarta.inject.Singleton | ||
import org.apache.commons.lang3.exception.ExceptionUtils | ||
|
||
/** [ExceptionHandler] applies all available [ExceptionClassifier] implementations in sequence. */ | ||
@Singleton | ||
class ExceptionHandler(val classifiers: List<ExceptionClassifier>) { | ||
|
||
fun classify(e: Throwable): ConnectorError { | ||
for (classifier in classifiers) { | ||
val classified: ConnectorError? = classifier.classify(e) | ||
if (classified != null) { | ||
return classified | ||
} | ||
} | ||
return SystemError(e.message) | ||
} | ||
|
||
/** Maps [e] to a [AirbyteErrorTraceMessage] to be passed to the [OutputConsumer]. */ | ||
fun handle(e: Throwable): AirbyteErrorTraceMessage { | ||
ApmTraceUtils.addExceptionToTrace(e) | ||
val errorTraceMessage = | ||
AirbyteErrorTraceMessage() | ||
.withInternalMessage(e.toString()) | ||
.withStackTrace(ExceptionUtils.getStackTrace(e)) | ||
return when (val classified: ConnectorError = classify(e)) { | ||
is ConfigError -> | ||
errorTraceMessage | ||
.withFailureType(AirbyteErrorTraceMessage.FailureType.CONFIG_ERROR) | ||
.withMessage(classified.displayMessage) | ||
is TransientError -> | ||
errorTraceMessage | ||
.withFailureType(AirbyteErrorTraceMessage.FailureType.TRANSIENT_ERROR) | ||
.withMessage(classified.displayMessage) | ||
is SystemError -> | ||
errorTraceMessage | ||
.withFailureType(AirbyteErrorTraceMessage.FailureType.SYSTEM_ERROR) | ||
.withMessage(classified.displayMessage ?: e.message) | ||
} | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
airbyte-cdk/bulk/core/base/src/main/kotlin/io/airbyte/cdk/output/RegexExceptionClassifier.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright (c) 2024 Airbyte, Inc., all rights reserved. | ||
*/ | ||
|
||
package io.airbyte.cdk.output | ||
|
||
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings | ||
import io.micronaut.context.annotation.EachProperty | ||
import io.micronaut.context.annotation.Parameter | ||
import io.micronaut.context.annotation.Requires | ||
import io.micronaut.context.annotation.Value | ||
import jakarta.inject.Singleton | ||
|
||
const val REGEX_CLASSIFIER_PREFIX = "${EXCEPTION_CLASSIFIER_PREFIX}.regex" | ||
|
||
/** [ExceptionClassifier] implementation based on regexes applied to the exception message. */ | ||
@Singleton | ||
@Requires(property = "${REGEX_CLASSIFIER_PREFIX}.rules") | ||
class RegexExceptionClassifier( | ||
@Value("\${${REGEX_CLASSIFIER_PREFIX}.order:10}") override val orderValue: Int, | ||
override val rules: List<RegexExceptionClassifierRule>, | ||
) : RuleBasedExceptionClassifier<RegexExceptionClassifierRule> { | ||
|
||
init { | ||
for (rule in rules) { | ||
rule.validate() | ||
} | ||
} | ||
} | ||
|
||
/** Micronaut configuration object for [RuleBasedExceptionClassifier] rules. */ | ||
@EachProperty("${REGEX_CLASSIFIER_PREFIX}.rules", list = true) | ||
@SuppressFBWarnings(value = ["NP_NONNULL_RETURN_VIOLATION"], justification = "Micronaut DI") | ||
class RegexExceptionClassifierRule( | ||
@param:Parameter override val ordinal: Int, | ||
) : RuleBasedExceptionClassifier.Rule { | ||
|
||
// Micronaut configuration objects work better with mutable properties. | ||
override lateinit var error: RuleBasedExceptionClassifier.ErrorKind | ||
lateinit var pattern: String | ||
lateinit var inputExample: String | ||
override var group: String? = null | ||
override var output: String? = null | ||
override var referenceLinks: List<String> = emptyList() | ||
|
||
val regex: Regex by lazy { | ||
pattern.toRegex(setOf(RegexOption.MULTILINE, RegexOption.IGNORE_CASE)) | ||
} | ||
|
||
override fun matches(e: Throwable): Boolean = | ||
e.message?.let { regex.containsMatchIn(it) } ?: false | ||
|
||
override fun validate() { | ||
require(runCatching { error }.isSuccess) { "error kind must be set" } | ||
require(runCatching { pattern }.isSuccess) { "regex pattern must be set" } | ||
require(runCatching { inputExample }.isSuccess) { | ||
"input exception message example must be set" | ||
} | ||
val compileResult: Result<Regex> = runCatching { regex } | ||
require(compileResult.isSuccess) { | ||
"regex pattern error: ${compileResult.exceptionOrNull()?.message}" | ||
} | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
...dk/bulk/core/base/src/test/kotlin/io/airbyte/cdk/output/DefaultExceptionClassifierTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright (c) 2024 Airbyte, Inc., all rights reserved. | ||
*/ | ||
|
||
package io.airbyte.cdk.output | ||
|
||
import io.airbyte.cdk.ConfigErrorException | ||
import io.airbyte.cdk.SystemErrorException | ||
import io.airbyte.cdk.TransientErrorException | ||
import io.micronaut.test.extensions.junit5.annotation.MicronautTest | ||
import jakarta.inject.Inject | ||
import org.junit.jupiter.api.Assertions | ||
import org.junit.jupiter.api.Test | ||
|
||
@MicronautTest | ||
class DefaultExceptionClassifierTest { | ||
|
||
@Inject lateinit var classifier: DefaultExceptionClassifier | ||
|
||
@Test | ||
fun testConfigError() { | ||
Assertions.assertEquals( | ||
ConfigError("foo"), | ||
classifier.classify(ConfigErrorException("foo")), | ||
) | ||
} | ||
|
||
@Test | ||
fun testTransientError() { | ||
Assertions.assertEquals( | ||
TransientError("bar"), | ||
classifier.classify(TransientErrorException("bar")), | ||
) | ||
} | ||
|
||
@Test | ||
fun testSystemError() { | ||
Assertions.assertEquals( | ||
SystemError("baz"), | ||
classifier.classify(SystemErrorException("baz")), | ||
) | ||
} | ||
|
||
@Test | ||
fun testUnclassified() { | ||
Assertions.assertNull(classifier.classify(RuntimeException("quux"))) | ||
} | ||
} |
Oops, something went wrong.