Skip to content

Commit fed92e8

Browse files
committed
error callbacks
1 parent e703d04 commit fed92e8

File tree

4 files changed

+20
-4
lines changed

4 files changed

+20
-4
lines changed

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

+2-2
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

+2-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ private inline fun fetch(
7575
Result.failure(
7676
FeaturevisorError.UnparsableJson(
7777
responseBody.string(),
78-
response.message
78+
response.message,
79+
response.code
7980
)
8081
)
8182
)

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ class FeaturevisorInstance private constructor(options: InstanceOptions) {
117117
if (refreshInterval != null) startRefreshing()
118118
}.onFailure { error ->
119119
logger?.error("Failed to fetch datafile: $error")
120-
emitter.emit(ERROR)
120+
emitter.emit(ERROR, error)
121121
}
122122
cancelFetchJob()
123123
}

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

+15
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)