Skip to content

Commit

Permalink
[cdk, source-postgres, source-mysql] a new error handling and transla…
Browse files Browse the repository at this point in the history
…tion framework (#40208)

Fixes airbytehq/airbyte-internal-issues#8516

This set of changes mainly moves error translation to be part of each connector.

In general, each connector needs to implement its own error translation class that inherits from the abstract class ConnectorExceptionTranslator, which is part of the CDK. By implementing, it means the connector developer or our support will populate the error dictionary with error samples with matching rules (e.g., regex). See the example we created for the Postgres source.
  • Loading branch information
theyueli authored Jul 18, 2024
1 parent 824b79c commit 4417769
Show file tree
Hide file tree
Showing 20 changed files with 781 additions and 648 deletions.
536 changes: 268 additions & 268 deletions airbyte-cdk/java/airbyte-cdk/README.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import com.google.common.annotations.VisibleForTesting
import com.google.common.base.Preconditions
import com.google.common.collect.Lists
import datadog.trace.api.Trace
import io.airbyte.cdk.integrations.util.ApmTraceUtils
import io.airbyte.cdk.integrations.util.ConnectorExceptionUtil
import io.airbyte.cdk.integrations.util.ConnectorExceptionHandler
import io.airbyte.cdk.integrations.util.concurrent.ConcurrentStreamConsumer
import io.airbyte.commons.features.EnvVariableFeatureFlags
import io.airbyte.commons.features.FeatureFlags
Expand Down Expand Up @@ -110,17 +109,24 @@ internal constructor(

@Trace(operationName = "RUN_OPERATION")
@Throws(Exception::class)
fun run(args: Array<String>) {
@JvmOverloads
fun run(
args: Array<String>,
exceptionHandler: ConnectorExceptionHandler = ConnectorExceptionHandler()
) {
val parsed = cliParser.parse(args)
try {
runInternal(parsed)
runInternal(parsed, exceptionHandler)
} catch (e: Exception) {
throw e
}
}

@Throws(Exception::class)
private fun runInternal(parsed: IntegrationConfig) {
private fun runInternal(
parsed: IntegrationConfig,
exceptionHandler: ConnectorExceptionHandler
) {
LOGGER.info { "Running integration: ${integration.javaClass.name}" }
LOGGER.info { "Command: ${parsed.command}" }
LOGGER.info { "Integration config: $parsed" }
Expand Down Expand Up @@ -213,59 +219,8 @@ internal constructor(
}
}
} catch (e: Exception) {
LOGGER.error(e) { "caught exception!" }
// Many of the exceptions thrown are nested inside layers of RuntimeExceptions. An
// attempt is made
// to
// find the root exception that corresponds to a configuration error. If that does not
// exist, we
// just return the original exception.
ApmTraceUtils.addExceptionToTrace(e)
val rootConfigErrorThrowable = ConnectorExceptionUtil.getRootConfigError(e)
val rootTransientErrorThrowable = ConnectorExceptionUtil.getRootTransientError(e)
// If the source connector throws a config error, a trace message with the relevant
// message should
// be surfaced.
if (parsed.command == Command.CHECK) {
// Currently, special handling is required for the CHECK case since the user display
// information in
// the trace message is
// not properly surfaced to the FE. In the future, we can remove this and just throw
// an exception.
outputRecordCollector.accept(
AirbyteMessage()
.withType(AirbyteMessage.Type.CONNECTION_STATUS)
.withConnectionStatus(
AirbyteConnectionStatus()
.withStatus(AirbyteConnectionStatus.Status.FAILED)
.withMessage(
ConnectorExceptionUtil.getDisplayMessage(
rootConfigErrorThrowable
)
)
)
)
return
}

if (ConnectorExceptionUtil.isConfigError(rootConfigErrorThrowable)) {
AirbyteTraceMessageUtility.emitConfigErrorTrace(
e,
ConnectorExceptionUtil.getDisplayMessage(rootConfigErrorThrowable),
)
// On receiving a config error, the container should be immediately shut down.
System.exit(1)
} else if (ConnectorExceptionUtil.isTransientError(rootTransientErrorThrowable)) {
AirbyteTraceMessageUtility.emitTransientErrorTrace(
e,
ConnectorExceptionUtil.getDisplayMessage(rootTransientErrorThrowable)
)
// On receiving a transient error, the container should be immediately shut down.
System.exit(1)
}
throw e
exceptionHandler.handleException(e, parsed.command, outputRecordCollector)
}

LOGGER.info { "Completed integration: ${integration.javaClass.name}" }
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
/*
* Copyright (c) 2024 Airbyte, Inc., all rights reserved.
*/
package io.airbyte.cdk.integrations.util

import io.airbyte.cdk.integrations.base.AirbyteTraceMessageUtility
import io.airbyte.cdk.integrations.base.Command
import io.airbyte.cdk.integrations.base.errors.messages.ErrorMessage
import io.airbyte.commons.exceptions.ConfigErrorException
import io.airbyte.commons.exceptions.ConnectionErrorException
import io.airbyte.commons.exceptions.TransientErrorException
import io.airbyte.protocol.models.v0.AirbyteConnectionStatus
import io.airbyte.protocol.models.v0.AirbyteMessage
import io.github.oshai.kotlinlogging.KotlinLogging
import java.util.function.Consumer
import java.util.regex.Pattern
import java.util.regex.PatternSyntaxException
import kotlin.system.exitProcess
import org.jetbrains.annotations.VisibleForTesting

private val LOGGER = KotlinLogging.logger {}

enum class FailureType {
CONFIG,
TRANSIENT
}

data class ConnectorErrorProfile(
val errorClass: String,
val regexMatchingPattern: String,
val failureType: FailureType,
val externalMessage: String,
val sampleInternalMessage: String,
val referenceLinks: List<String> = emptyList(),
) {
init {
require(isValidRegex(regexMatchingPattern)) {
"regexMatchingPattern is not a valid regular expression string"
}
require(externalMessage.isNotBlank()) { "externalMessage must not be blank" }
require(sampleInternalMessage.isNotBlank()) { "sampleInternalMessage must not be blank" }
}

private fun isValidRegex(regexString: String): Boolean {
return try {
Pattern.compile(regexString)
true
} catch (e: PatternSyntaxException) {
false
}
}
}

/**
* This class defines interfaces that will be implemented by individual connectors for translating
* internal exception error messages to external user-friendly error messages.
*/
open class ConnectorExceptionHandler {
private val COMMON_EXCEPTION_MESSAGE_TEMPLATE: String =
"Could not connect with provided configuration. Error: %s"

protected open val connectorErrorDictionary: MutableList<ConnectorErrorProfile> =
mutableListOf()

init {
initializeErrorDictionary()
}

/**
* Handles exceptions thrown by the connector. This method is the main entrance for handling
* exceptions thrown by the connector. It checks if the exception is a known exception, and if
* so, it emits the appropriate trace and external user-friendly error message. If the exception
* is not known, it rethrows the exception, which becomes a system error.
*/
fun handleException(
e: Throwable,
cmd: Command,
outputRecordCollector: Consumer<AirbyteMessage>
) {
LOGGER.error(e) { "caught exception!" }
ApmTraceUtils.addExceptionToTrace(e)
val rootException: Throwable = getRootException(e)
val externalMessage: String? = getExternalMessage(rootException)
/* error messages generated during check() needs special handling */
if (cmd == Command.CHECK) {
outputRecordCollector.accept(
AirbyteMessage()
.withType(AirbyteMessage.Type.CONNECTION_STATUS)
.withConnectionStatus(
AirbyteConnectionStatus()
.withStatus(AirbyteConnectionStatus.Status.FAILED)
.withMessage(externalMessage),
),
)
} else {
if (checkErrorType(rootException, FailureType.CONFIG)) {
AirbyteTraceMessageUtility.emitConfigErrorTrace(e, externalMessage)
exitProcess(1)
} else if (checkErrorType(rootException, FailureType.TRANSIENT)) {
AirbyteTraceMessageUtility.emitTransientErrorTrace(e, externalMessage)
exitProcess(1)
}
throw e
}
}

/**
* Initializes the error dictionary for the connector. This method shall include all the errors
* that are shared by all connectors.
*/
open fun initializeErrorDictionary() {}

/**
* Translates an internal exception message to an external user-friendly message. This is the
* main entrance of the error translation process.
*/
fun getExternalMessage(e: Throwable?): String? {
// some common translations that every connector would share can be done here
if (e is ConfigErrorException) {
return e.displayMessage
} else if (e is TransientErrorException) {
return e.message
} else if (e is ConnectionErrorException) {
return ErrorMessage.getErrorMessage(e.stateCode, e.errorCode, e.exceptionMessage, e)
} else {
val msg = translateConnectorSpecificErrorMessage(e)
if (msg != null) return msg
}
// if no specific translation is found, return a generic message
return String.format(
COMMON_EXCEPTION_MESSAGE_TEMPLATE,
if (e!!.message != null) e.message else "",
)
}

fun add(errorProfile: ConnectorErrorProfile) {
connectorErrorDictionary.add(errorProfile)
}

/**
* Translates a connector specific error message to an external user-friendly message. This
* method should be implemented by individual connectors that wish to translate connector
* specific error messages.
*/
open fun translateConnectorSpecificErrorMessage(e: Throwable?): String? {
if (e == null) return null
for (error in connectorErrorDictionary) {
if (e.message?.lowercase()?.matches(error.regexMatchingPattern.toRegex())!!)
return error.externalMessage
}
return null
}

/**
* Many of the exceptions thrown are nested inside layers of RuntimeExceptions. An attempt is
* made to find the root exception that corresponds to a configuration error. If that does not
* exist, we just return the original exception.
*/
@VisibleForTesting
internal fun getRootException(e: Throwable): Throwable {
var current: Throwable? = e
while (current != null) {
if (isRecognizableError(current)) {
return current
} else {
current = current.cause
}
}
return e
}

private fun checkErrorType(e: Throwable?, failureType: FailureType?): Boolean {
for (error in connectorErrorDictionary) {
if (
error.failureType == failureType &&
e!!.message?.matches(error.regexMatchingPattern.toRegex())!!
)
return true
}
return false
}

/*
* Checks if the error can be recognized. A recognizable error is either
* a known transient exception, a config exception, or an exception whose error messages have been
* stored as part of the error profile in the error dictionary.
* */
private fun isRecognizableError(e: Throwable?): Boolean {
if (e == null) return false
if (e is TransientErrorException || e is ConfigErrorException) {
return true
}
for (error in connectorErrorDictionary) {
if (e.message?.matches(error.regexMatchingPattern.toRegex())!!) return true
}
return false
}
}
Loading

0 comments on commit 4417769

Please sign in to comment.