Skip to content

Commit 72085ac

Browse files
refactor: simplify response code retrieval in EventUploadError
1 parent ff2f914 commit 72085ac

File tree

2 files changed

+12
-18
lines changed

2 files changed

+12
-18
lines changed

core/src/main/kotlin/com/rudderstack/sdk/kotlin/core/internals/network/EventUploadResult.kt

+9-15
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,13 @@ internal data class Success(val response: String) : EventUploadResult
1717
/**
1818
* `EventUploadError` is a sealed interface representing an error that occurred during event upload.
1919
* It can be either a retry able error or a non-retry able error.
20+
*
21+
* @property responseCode The HTTP response code associated with the error, if available.
2022
*/
21-
internal sealed interface EventUploadError : EventUploadResult
23+
internal sealed interface EventUploadError : EventUploadResult {
24+
25+
val responseCode: Int?
26+
}
2227

2328
/**
2429
* `RetryAbleError` is a sealed interface representing an event upload error that can be retried.
@@ -35,7 +40,7 @@ internal sealed interface NonRetryAbleError : EventUploadError
3540
*
3641
* @property responseCode The HTTP response code associated with the error, if available.
3742
*/
38-
internal sealed class RetryAbleEventUploadError(open val responseCode: Int? = null) : RetryAbleError {
43+
internal sealed class RetryAbleEventUploadError(override val responseCode: Int? = null) : RetryAbleError {
3944

4045
/**
4146
* `ErrorRetry` represents a retry able error with a specific HTTP response code.
@@ -61,7 +66,7 @@ internal sealed class RetryAbleEventUploadError(open val responseCode: Int? = nu
6166
* @property ERROR_404 An error indicating that the resource was not found (e.g., the source is disabled).
6267
* @property ERROR_413 An error indicating that the payload size exceeds the maximum allowed limit.
6368
*/
64-
internal enum class NonRetryAbleEventUploadError(val responseCode: Int) : NonRetryAbleError {
69+
internal enum class NonRetryAbleEventUploadError(override val responseCode: Int) : NonRetryAbleError {
6570

6671
ERROR_400(responseCode = HTTP_400),
6772
ERROR_401(responseCode = HTTP_401),
@@ -93,22 +98,11 @@ internal fun NetworkResult.toEventUploadResult(): EventUploadResult {
9398
}
9499
}
95100

96-
/**
97-
* Extension function that retrieves the response code from a RetryAbleError.
98-
* @return The HTTP response code associated with this error, or null if not available.
99-
*/
100-
internal fun EventUploadError.getResponseCode(): Int? {
101-
return when (this) {
102-
is RetryAbleEventUploadError -> this.responseCode
103-
is NonRetryAbleEventUploadError -> this.responseCode
104-
}
105-
}
106-
107101
/**
108102
* Extension function that formats the error's response code as a message string.
109103
* @return A string representation of the response code, or "Not available" if the code is null.
110104
*/
111105
internal fun EventUploadError.formatResponseCodeMessage(): String {
112-
val responseCode = this.getResponseCode() ?: "Not available"
106+
val responseCode = this.responseCode ?: "Not available"
113107
return "Response code: $responseCode"
114108
}

core/src/test/kotlin/com/rudderstack/sdk/kotlin/core/internals/network/EventUploadResultTest.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ internal class EventUploadResultTest {
5757
error: EventUploadError,
5858
expectedResponseCode: Int
5959
) {
60-
val responseCode = error.getResponseCode()
60+
val responseCode = error.responseCode
6161

6262
assertEquals(expectedResponseCode, responseCode)
6363
}
@@ -67,7 +67,7 @@ internal class EventUploadResultTest {
6767
fun `when retry able error is provided, then it returns null response code`(
6868
retryAbleError: RetryAbleEventUploadError
6969
) {
70-
val responseCode = retryAbleError.getResponseCode()
70+
val responseCode = retryAbleError.responseCode
7171

7272
assertNull(responseCode)
7373
}
@@ -80,7 +80,7 @@ internal class EventUploadResultTest {
8080
val eventUploadResult = networkResult.toEventUploadResult()
8181

8282
assertTrue(eventUploadResult is RetryAbleEventUploadError)
83-
val actualResponseCode = (eventUploadResult as RetryAbleEventUploadError).getResponseCode()
83+
val actualResponseCode = (eventUploadResult as RetryAbleEventUploadError).responseCode
8484
assertEquals(errorCode, actualResponseCode)
8585
}
8686

0 commit comments

Comments
 (0)