diff --git a/src/main/kotlin/com/featurevisor/sdk/Instance+Fetch.kt b/src/main/kotlin/com/featurevisor/sdk/Instance+Fetch.kt index 025fd2c..ea950c8 100644 --- a/src/main/kotlin/com/featurevisor/sdk/Instance+Fetch.kt +++ b/src/main/kotlin/com/featurevisor/sdk/Instance+Fetch.kt @@ -56,8 +56,19 @@ private inline fun fetch( } val responseBodyString = responseBody.string() FeaturevisorInstance.companionLogger?.debug(responseBodyString) - val content = json.decodeFromString(responseBodyString) - completion(Result.success(content)) + try { + val content = json.decodeFromString(responseBodyString) + completion(Result.success(content)) + } catch(throwable: Throwable) { + completion( + Result.failure( + FeaturevisorError.UnparsableJson( + responseBody.string(), + response.message + ) + ) + ) + } } else { completion( Result.failure( diff --git a/src/main/kotlin/com/featurevisor/sdk/Instance.kt b/src/main/kotlin/com/featurevisor/sdk/Instance.kt index 3a7fb14..8dcc271 100644 --- a/src/main/kotlin/com/featurevisor/sdk/Instance.kt +++ b/src/main/kotlin/com/featurevisor/sdk/Instance.kt @@ -10,10 +10,7 @@ import com.featurevisor.types.BucketValue import com.featurevisor.types.Context import com.featurevisor.types.DatafileContent import com.featurevisor.types.EventName -import com.featurevisor.types.EventName.ACTIVATION -import com.featurevisor.types.EventName.READY -import com.featurevisor.types.EventName.REFRESH -import com.featurevisor.types.EventName.UPDATE +import com.featurevisor.types.EventName.* import com.featurevisor.types.Feature import com.featurevisor.types.StickyFeatures import kotlinx.coroutines.Job @@ -81,6 +78,11 @@ class FeaturevisorInstance private constructor(options: InstanceOptions) { ACTIVATION, onActivation ) } + if (onError != null) { + emitter.addListener( + ERROR, onError + ) + } on = emitter::addListener off = emitter::removeListener @@ -104,7 +106,7 @@ class FeaturevisorInstance private constructor(options: InstanceOptions) { if (refreshInterval != null) startRefreshing() } else { logger?.error("Failed to fetch datafile: $result") - throw FetchingDataFileFailed(result.toString()) + emitter.emit(ERROR) } } } diff --git a/src/main/kotlin/com/featurevisor/sdk/InstanceOptions.kt b/src/main/kotlin/com/featurevisor/sdk/InstanceOptions.kt index 86e72bd..48254db 100644 --- a/src/main/kotlin/com/featurevisor/sdk/InstanceOptions.kt +++ b/src/main/kotlin/com/featurevisor/sdk/InstanceOptions.kt @@ -20,6 +20,7 @@ data class InstanceOptions( val onReady: Listener? = null, val onRefresh: Listener? = null, val onUpdate: Listener? = null, + val onError: Listener? = null, val refreshInterval: Long? = null, // seconds val stickyFeatures: StickyFeatures? = null, ) { diff --git a/src/main/kotlin/com/featurevisor/types/Types.kt b/src/main/kotlin/com/featurevisor/types/Types.kt index 703ac0f..862f296 100644 --- a/src/main/kotlin/com/featurevisor/types/Types.kt +++ b/src/main/kotlin/com/featurevisor/types/Types.kt @@ -304,6 +304,7 @@ enum class EventName { REFRESH, UPDATE, ACTIVATION, + ERROR, } /**