Skip to content

Commit 2724ddf

Browse files
committed
Add error call back
1 parent ca52f6b commit 2724ddf

File tree

4 files changed

+27
-4
lines changed

4 files changed

+27
-4
lines changed

src/main/kotlin/com/featurevisor/sdk/FeaturevisorError.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.featurevisor.sdk
22

3-
sealed class FeaturevisorError(message: String) : Throwable(message = message) {
3+
sealed class FeaturevisorError(message: String, var code: Int = 0) : Throwable(message = message) {
44

55
/// Thrown when attempting to init Featurevisor instance without passing datafile and datafileUrl.
66
/// At least one of them is required to init the SDK correctly
@@ -12,7 +12,7 @@ sealed class FeaturevisorError(message: String) : Throwable(message = message) {
1212
/// - Parameters:
1313
/// - data: The data being parsed.
1414
/// - errorMessage: The message from the error which occured during parsing.
15-
class UnparsableJson(val data: String?, errorMessage: String) : FeaturevisorError(errorMessage)
15+
class UnparsableJson(val data: String?, errorMessage: String, code: Int = 0) : FeaturevisorError(errorMessage, code)
1616

1717
/// Thrown when attempting to construct an invalid URL.
1818
/// - Parameter string: The invalid URL string.

src/main/kotlin/com/featurevisor/sdk/Instance+Fetch.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,15 @@ private suspend fun fetchWithRetry(
148148
logger?.error("Request failed with message: ${response.message}")
149149
delay(retryInterval)
150150
} else {
151-
completion(Result.failure(FeaturevisorError.UnparsableJson(responseBodyString, response.message)))
151+
completion(
152+
Result.failure(
153+
FeaturevisorError.UnparsableJson(
154+
responseBodyString,
155+
response.message,
156+
response.code,
157+
)
158+
)
159+
)
152160
}
153161
}
154162
} catch (e: IOException) {

src/main/kotlin/com/featurevisor/sdk/Instance.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ class FeaturevisorInstance private constructor(options: InstanceOptions) {
122122
if (refreshInterval != null) startRefreshing()
123123
}.onFailure { error ->
124124
logger?.error("Failed to fetch datafile: $error")
125-
emitter.emit(ERROR)
125+
emitter.emit(ERROR, error)
126126
}
127127
cancelFetchRetry()
128128
}

src/test/kotlin/com/featurevisor/sdk/EmitterTest.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import com.featurevisor.types.EventName.ACTIVATION
44
import com.featurevisor.types.EventName.READY
55
import com.featurevisor.types.EventName.REFRESH
66
import com.featurevisor.types.EventName.UPDATE
7+
import com.featurevisor.types.EventName.ERROR
78
import com.featurevisor.types.EventName.values
89
import io.mockk.every
910
import io.mockk.mockk
@@ -24,6 +25,9 @@ class EmitterTest {
2425
private val activationCallback: Listener = mockk {
2526
every { this@mockk(emptyArray()) } answers { nothing }
2627
}
28+
private val errorCallback: Listener = mockk {
29+
every { this@mockk(any()) } answers { nothing }
30+
}
2731

2832
private val systemUnderTest = Emitter()
2933

@@ -85,4 +89,15 @@ class EmitterTest {
8589
activationCallback(any())
8690
}
8791
}
92+
93+
@Test
94+
fun `add error listener and confirm it is invoked`() {
95+
systemUnderTest.addListener(ERROR, errorCallback)
96+
97+
systemUnderTest.emit(ERROR, "data")
98+
99+
verify(exactly = 1) {
100+
errorCallback(arrayOf("data"))
101+
}
102+
}
88103
}

0 commit comments

Comments
 (0)