Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.spotify.confidence

import android.content.Context
import com.spotify.confidence.ConfidenceError.HttpError
import com.spotify.confidence.ConfidenceError.ParseError
import com.spotify.confidence.apply.FlagApplierWithRetries
import com.spotify.confidence.cache.DiskStorage
Expand Down Expand Up @@ -287,6 +288,8 @@ class Confidence internal constructor(
}
} catch (e: ParseError) {
throw ParseError(e.message)
} catch (e: HttpError) {
throw e
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,10 @@ class ConfidenceError {
val flag: String
) : Error(message)

data class HttpError(
val httpCode: Int,
override val message: String
) : Error(message)

class InvalidContextInMessage : Error("Field 'context' is not allowed in event's data")
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,20 @@ internal class EventSenderUploaderImpl(
.post(networkJson.encodeToString(events).toRequestBody())
.build()

val response = httpClient.newCall(httpRequest).await()
if (!response.isSuccessful) {
debugLogger?.logError(message = "Failed to upload events. http code ${response.code}")
}
when (response.code) {
// clean up in case of success
200 -> true
// we shouldn't cleanup for rate limiting
// TODO("return retry-after")
429 -> false
// if batch couldn't be processed, we should clean it up
in 400..499 -> true
else -> false
httpClient.newCall(httpRequest).await().use { response ->
if (!response.isSuccessful) {
debugLogger?.logError(message = "Failed to upload events. http code ${response.code}")
}
when (response.code) {
// clean up in case of success
200 -> true
// we shouldn't cleanup for rate limiting
// TODO("return retry-after")
429 -> false
// if batch couldn't be processed, we should clean it up
in 400..499 -> true
else -> false
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ internal class RemoteFlagResolver(
val startTime = System.nanoTime()
var status = Telemetry.RequestStatus.SUCCESS
try {
val result = httpClient.newCall(httpRequest).await()
result.toResolveFlags()
httpClient.newCall(httpRequest).await().use { it.toResolveFlags() }
} catch (e: SocketTimeoutException) {
status = Telemetry.RequestStatus.TIMEOUT
throw e
Expand Down Expand Up @@ -92,22 +91,16 @@ internal class RemoteFlagResolver(
private fun Response.toResolveFlags(): ResolveResponse {
if (!isSuccessful) {
debugLogger?.logError("Failed to resolve flags. Http code: $code")
throw ConfidenceError.HttpError(code, "Failed to resolve flags. Http code: $code")
}
body?.let { body ->
val bodyString = body.string()

// building the json class responsible for serializing the object
val networkJson = Json {
serializersModule = SerializersModule {
ignoreUnknownKeys = true
}
val bodyString = body?.string()
?: throw ConfidenceError.ParseError("Response body is null", listOf())
val networkJson = Json {
serializersModule = SerializersModule {
ignoreUnknownKeys = true
}
try {
return ResolveResponse.Resolved(networkJson.decodeFromString(bodyString))
} finally {
body.close()
}
} ?: throw ConfidenceError.ParseError("Response body is null", listOf())
}
return ResolveResponse.Resolved(networkJson.decodeFromString(bodyString))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,16 @@ internal class FlagApplierClientImpl : FlagApplierClient {
extraHeaders[Telemetry.HEADER_NAME] = headerValue
}

val result = applyInteractor.invoke(request, extraHeaders).runCatching {
if (isSuccessful) {
Result.Success(Unit)
} else {
Result.Failure()
return try {
applyInteractor.invoke(request, extraHeaders).use { response ->
if (response.isSuccessful) {
Result.Success(Unit)
} else {
Result.Failure()
}
}
}.getOrElse {
} catch (_: Exception) {
Result.Failure()
}

return result
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package com.spotify.confidence.openfeature
import com.spotify.confidence.Confidence
import com.spotify.confidence.ConfidenceError.ErrorCode
import com.spotify.confidence.ConfidenceError.FlagNotFoundError
import com.spotify.confidence.ConfidenceError.HttpError
import com.spotify.confidence.ConfidenceError.ParseError
import com.spotify.confidence.ConfidenceValue
import com.spotify.confidence.Evaluation
Expand Down Expand Up @@ -125,6 +126,8 @@ class ConfidenceFeatureProvider private constructor(
throw OpenFeatureError.ParseError(e.message)
} catch (e: FlagNotFoundError) {
throw OpenFeatureError.FlagNotFoundError(e.flag)
} catch (e: HttpError) {
throw OpenFeatureError.GeneralError(e.message)
}
}
companion object {
Expand Down
Loading