diff --git a/core/src/main/java/com/segment/analytics/kotlin/core/Errors.kt b/core/src/main/java/com/segment/analytics/kotlin/core/Errors.kt index ef2348c6..d093d25d 100644 --- a/core/src/main/java/com/segment/analytics/kotlin/core/Errors.kt +++ b/core/src/main/java/com/segment/analytics/kotlin/core/Errors.kt @@ -1,6 +1,8 @@ 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() @@ -8,18 +10,24 @@ sealed class AnalyticsError(): Throwable() { 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() } /** diff --git a/core/src/main/java/com/segment/analytics/kotlin/core/Settings.kt b/core/src/main/java/com/segment/analytics/kotlin/core/Settings.kt index 97799fb4..197dafa4 100644 --- a/core/src/main/java/com/segment/analytics/kotlin/core/Settings.kt +++ b/core/src/main/java/com/segment/analytics/kotlin/core/Settings.kt @@ -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 @@ -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( @@ -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 -} \ No newline at end of file + 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 + } \ No newline at end of file