Skip to content

Commit

Permalink
Fix error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
uniumuniu committed Dec 28, 2023
1 parent e336dfc commit 2229f2d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
15 changes: 13 additions & 2 deletions src/main/kotlin/com/featurevisor/sdk/Instance+Fetch.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,19 @@ private inline fun fetch(
}
val responseBodyString = responseBody.string()
FeaturevisorInstance.companionLogger?.debug(responseBodyString)
val content = json.decodeFromString<DatafileContent>(responseBodyString)
completion(Result.success(content))
try {
val content = json.decodeFromString<DatafileContent>(responseBodyString)
completion(Result.success(content))
} catch(throwable: Throwable) {
completion(
Result.failure(
FeaturevisorError.UnparsableJson(
responseBody.string(),
response.message
)
)
)
}
} else {
completion(
Result.failure(
Expand Down
12 changes: 7 additions & 5 deletions src/main/kotlin/com/featurevisor/sdk/Instance.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/main/kotlin/com/featurevisor/sdk/InstanceOptions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
) {
Expand Down
1 change: 1 addition & 0 deletions src/main/kotlin/com/featurevisor/types/Types.kt
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ enum class EventName {
REFRESH,
UPDATE,
ACTIVATION,
ERROR,
}

/**
Expand Down

0 comments on commit 2229f2d

Please sign in to comment.