Skip to content

Commit

Permalink
Error handling parity (#237)
Browse files Browse the repository at this point in the history
* feat: update AnalyticsErrors to match Swift version.

* feat: wrap NetworkUnknown error in a SettingsFail Error.
  • Loading branch information
didiergarcia authored Oct 24, 2024
1 parent 745c7a4 commit 1ef79ea
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 27 deletions.
30 changes: 19 additions & 11 deletions core/src/main/java/com/segment/analytics/kotlin/core/Errors.kt
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
package com.segment.analytics.kotlin.core

import com.segment.analytics.kotlin.core.platform.plugins.logger.segmentLog
import java.net.URL

sealed class AnalyticsError(): Throwable() {
data class StorageUnableToCreate(override val message: String?): AnalyticsError()
data class StorageUnableToWrite(override val message: String?): AnalyticsError()
data class StorageUnableToRename(override val message: String?): AnalyticsError()
data class StorageUnableToOpen(override val message: String?): AnalyticsError()
data class StorageUnableToClose(override val message: String?): AnalyticsError()
data class StorageInvalid(override val message: String?): AnalyticsError()
data class StorageUnknown(override val message: String?, override val cause: Throwable?): AnalyticsError()
data class NetworkUnexpectedHTTPCode(override val message: String?): AnalyticsError()
data class NetworkServerLimited(override val message: String?): AnalyticsError()
data class NetworkServerRejected(override val message: String?): AnalyticsError()
data class NetworkUnknown(override val message: String?, override val cause: Throwable?): AnalyticsError()
data class StorageUnknown(override val cause: Throwable?): AnalyticsError()

data class NetworkUnexpectedHTTPCode(val uri: URL?, val code: Int): AnalyticsError()
data class NetworkServerLimited(val uri: URL?, val code: Int): AnalyticsError()
data class NetworkServerRejected(val uri: URL?, val code: Int): AnalyticsError()
data class NetworkUnknown(val uri: URL?, override val cause: Throwable?): AnalyticsError()
data class NetworkInvalidData(override val message: String?): AnalyticsError()
data class JsonUnableToSerialize(override val message: String?, override val cause: Throwable?): AnalyticsError()
data class JsonUnableToDeserialize(override val message: String?, override val cause: Throwable?): AnalyticsError()
data class JsonUnknown(override val message: String?, override val cause: Throwable?): AnalyticsError()
data class PluginError(override val message: String?, override val cause: Throwable?): AnalyticsError()
data class EnrichmentError(override val message: String?): AnalyticsError()
data class SettingsFetchError(override val message: String?, override val cause: Throwable?): AnalyticsError()

data class JsonUnableToSerialize(override val cause: Throwable?): AnalyticsError()
data class JsonUnableToDeserialize(override val cause: Throwable?): AnalyticsError()
data class JsonUnknown(override val cause: Throwable?): AnalyticsError()

data class PluginError(override val cause: Throwable?): AnalyticsError()

data class EnrichmentError(override val message: String): AnalyticsError()

data class SettingsFail(override val cause: AnalyticsError): AnalyticsError()
data class BatchUploadFail(override val cause: AnalyticsError): AnalyticsError()
}

/**
Expand Down
36 changes: 20 additions & 16 deletions core/src/main/java/com/segment/analytics/kotlin/core/Settings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package com.segment.analytics.kotlin.core

import com.segment.analytics.kotlin.core.platform.DestinationPlugin
import com.segment.analytics.kotlin.core.platform.Plugin
import com.segment.analytics.kotlin.core.platform.plugins.logger.LogKind
import com.segment.analytics.kotlin.core.platform.plugins.logger.log
import com.segment.analytics.kotlin.core.platform.plugins.logger.segmentLog
import com.segment.analytics.kotlin.core.utilities.LenientJson
import com.segment.analytics.kotlin.core.utilities.safeJsonObject
import kotlinx.coroutines.launch
Expand All @@ -15,6 +13,7 @@ import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.serializer
import java.io.BufferedReader
import java.net.URL

@Serializable
data class Settings(
Expand Down Expand Up @@ -109,17 +108,22 @@ internal fun Analytics.fetchSettings(
writeKey: String,
cdnHost: String
): Settings? = try {
val connection = HTTPClient(writeKey, this.configuration.requestFactory).settings(cdnHost)
val settingsString =
connection.inputStream?.bufferedReader()?.use(BufferedReader::readText) ?: ""
log("Fetched Settings: $settingsString")
LenientJson.decodeFromString(settingsString)
} catch (ex: Exception) {
reportErrorWithMetrics(this, AnalyticsError.SettingsFetchError(ex.message, ex), "Failed to fetch settings",
Telemetry.INVOKE_ERROR_METRIC, ex.stackTraceToString()) {
it["error"] = ex.toString()
it["writekey"] = writeKey
it["message"] = "Error retrieving settings"
}
configuration.defaultSettings
}
val connection = HTTPClient(writeKey, this.configuration.requestFactory).settings(cdnHost)
val settingsString =
connection.inputStream?.bufferedReader()?.use(BufferedReader::readText) ?: ""
log("Fetched Settings: $settingsString")
LenientJson.decodeFromString(settingsString)
} catch (ex: Exception) {
reportErrorWithMetrics(
this,
AnalyticsError.SettingsFail(AnalyticsError.NetworkUnknown(URL("https://$cdnHost/projects/$writeKey/settings"), ex)),
"Failed to fetch settings",
Telemetry.INVOKE_ERROR_METRIC,
ex.stackTraceToString()
) {
it["error"] = ex.toString()
it["writekey"] = writeKey
it["message"] = "Error retrieving settings"
}
configuration.defaultSettings
}

0 comments on commit 1ef79ea

Please sign in to comment.