Skip to content

Commit

Permalink
Avoid throwing exceptions up the stack
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanmos committed Feb 8, 2024
1 parent a65435f commit 67c70a6
Show file tree
Hide file tree
Showing 17 changed files with 250 additions and 154 deletions.
2 changes: 1 addition & 1 deletion dd-sdk-android-core/api/apiSurface
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ interface com.datadog.android.api.feature.StorageBackedFeature : Feature
data class com.datadog.android.api.net.Request
constructor(String, String, String, Map<String, String>, ByteArray, String? = null)
interface com.datadog.android.api.net.RequestFactory
fun create(com.datadog.android.api.context.DatadogContext, List<com.datadog.android.api.storage.RawBatchEvent>, ByteArray?): Request
fun create(com.datadog.android.api.context.DatadogContext, List<com.datadog.android.api.storage.RawBatchEvent>, ByteArray?): Request?
companion object
const val CONTENT_TYPE_JSON: String
const val CONTENT_TYPE_TEXT_UTF8: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fun interface RequestFactory {
context: DatadogContext,
batchData: List<RawBatchEvent>,
batchMetadata: ByteArray?
): Request
): Request?

companion object {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@ internal class DataOkHttpUploader(

// region DataUploader

@Suppress("TooGenericExceptionCaught")
@Suppress("TooGenericExceptionCaught", "ReturnCount")
override fun upload(
context: DatadogContext,
batch: List<RawBatchEvent>,
batchMeta: ByteArray?
): UploadStatus {
val request = try {
requestFactory.create(context, batch, batchMeta)
?: return UploadStatus.RequestCreationError
} catch (e: Exception) {
internalLogger.log(
InternalLogger.Level.ERROR,
Expand Down
8 changes: 8 additions & 0 deletions detekt_custom.yml
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@ datadog:
# endregion
# region Kotlin Collections
- "kotlin.Array.first(kotlin.Function1):java.util.NoSuchElementException"
- "kotlin.ByteArray.toRequestBody(okhttp3.MediaType?, kotlin.Int, kotlin.Int):java.lang.ArrayIndexOutOfBoundsException,java.io.IOException"
- "kotlin.String.toRequestBody(okhttp3.MediaType?):java.lang.ArrayIndexOutOfBoundsException,java.io.IOException"
- "kotlin.collections.List.last():java.util.NoSuchElementException"
- "kotlin.collections.List.first():java.util.NoSuchElementException"
- "kotlin.collections.List.get(kotlin.Int):java.util.NoSuchElementException"
Expand All @@ -247,6 +249,8 @@ datadog:
- "okhttp3.Call.execute():java.io.IOException"
- "okhttp3.Dns.lookup(kotlin.String):java.net.UnknownHostException"
- "okhttp3.HttpUrl.toUrl():java.lang.RuntimeException"
- "okhttp3.MultipartBody.Builder.build():java.lang.IllegalStateException"
- "okhttp3.MultipartBody.Builder.setType(okhttp3.MediaType):java.lang.IllegalArgumentException"
- "okhttp3.Response.peekBody(kotlin.Long):java.io.IOException,java.lang.IllegalArgumentException,java.lang.IllegalStateException"
- "okhttp3.RequestBody.writeTo(okio.BufferedSink):java.io.IOException"
- "okhttp3.Response.close():java.lang.IllegalStateException"
Expand All @@ -265,6 +269,7 @@ datadog:
- "okio.BufferedSink.close():java.io.IOException"
- "okio.Okio.buffer(okio.Sink):java.lang.NullPointerException"
- "okio.Buffer.readString(java.nio.charset.Charset):java.lang.IllegalArgumentException,java.io.IOException"
- "okio.Buffer.readByteArray():java.io.EOFException"
# endregion
# region org.json
- "org.json.JSONArray.get(kotlin.Int):org.json.JSONException"
Expand Down Expand Up @@ -1095,6 +1100,7 @@ datadog:
- "kotlin.String.toHttpUrlOrNull()"
- "kotlin.String.toIntOrNull()"
- "kotlin.String.toLongOrNull()"
- "kotlin.String.toMediaTypeOrNull()"
- "kotlin.String.toMethod()"
- "kotlin.String.toOperationType(com.datadog.android.api.InternalLogger)"
- "kotlin.String.uppercase(java.util.Locale)"
Expand Down Expand Up @@ -1156,6 +1162,8 @@ datadog:
- "okhttp3.HttpUrl.parse(kotlin.String)"
- "okhttp3.HttpUrl.url()"
- "okhttp3.Interceptor.Chain.request()"
- "okhttp3.MultipartBody.Builder.constructor(kotlin.String)"
- "okhttp3.MultipartBody.Builder.addFormDataPart(kotlin.String, kotlin.String?, okhttp3.RequestBody)"
- "okhttp3.MultipartBody.Part.body()"
- "okhttp3.MultipartBody.Part.headers()"
- "okhttp3.MultipartBody.parts()"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ internal class LogsRequestFactory(
context: DatadogContext,
batchData: List<RawBatchEvent>,
batchMetadata: ByteArray?
): Request {
): Request? {
val requestId = UUID.randomUUID().toString()

return Request(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,22 @@ internal class LogsRequestFactoryTest {
val request = testedFactory.create(fakeDatadogContext, batchData, batchMetadata)

// Then
assertThat(request.url).isEqualTo(
assertThat(request?.url).isEqualTo(
"${fakeDatadogContext.site.intakeEndpoint}/api/v2/logs?" +
"ddsource=${fakeDatadogContext.source}"
)
assertThat(request.contentType).isEqualTo(RequestFactory.CONTENT_TYPE_JSON)
assertThat(request.headers.minus(RequestFactory.HEADER_REQUEST_ID)).isEqualTo(
assertThat(request?.contentType).isEqualTo(RequestFactory.CONTENT_TYPE_JSON)
assertThat(request?.headers?.minus(RequestFactory.HEADER_REQUEST_ID)).isEqualTo(
mapOf(
RequestFactory.HEADER_API_KEY to fakeDatadogContext.clientToken,
RequestFactory.HEADER_EVP_ORIGIN to fakeDatadogContext.source,
RequestFactory.HEADER_EVP_ORIGIN_VERSION to fakeDatadogContext.sdkVersion
)
)
assertThat(request.headers[RequestFactory.HEADER_REQUEST_ID]).isNotEmpty()
assertThat(request.id).isEqualTo(request.headers[RequestFactory.HEADER_REQUEST_ID])
assertThat(request.description).isEqualTo("Logs Request")
assertThat(request.body).isEqualTo(
assertThat(request?.headers?.get(RequestFactory.HEADER_REQUEST_ID)).isNotEmpty()
assertThat(request?.id).isEqualTo(request?.headers?.get(RequestFactory.HEADER_REQUEST_ID))
assertThat(request?.description).isEqualTo("Logs Request")
assertThat(request?.body).isEqualTo(
batchData.map { it.data }
.join(
separator = ",".toByteArray(),
Expand Down Expand Up @@ -106,22 +106,22 @@ internal class LogsRequestFactoryTest {
val request = testedFactory.create(fakeDatadogContext, batchData, batchMetadata)

// Then
assertThat(request.url).isEqualTo(
assertThat(request?.url).isEqualTo(
"$fakeEndpoint/api/v2/logs?" +
"ddsource=${fakeDatadogContext.source}"
)
assertThat(request.contentType).isEqualTo(RequestFactory.CONTENT_TYPE_JSON)
assertThat(request.headers.minus(RequestFactory.HEADER_REQUEST_ID)).isEqualTo(
assertThat(request?.contentType).isEqualTo(RequestFactory.CONTENT_TYPE_JSON)
assertThat(request?.headers?.minus(RequestFactory.HEADER_REQUEST_ID)).isEqualTo(
mapOf(
RequestFactory.HEADER_API_KEY to fakeDatadogContext.clientToken,
RequestFactory.HEADER_EVP_ORIGIN to fakeDatadogContext.source,
RequestFactory.HEADER_EVP_ORIGIN_VERSION to fakeDatadogContext.sdkVersion
)
)
assertThat(request.headers[RequestFactory.HEADER_REQUEST_ID]).isNotEmpty()
assertThat(request.id).isEqualTo(request.headers[RequestFactory.HEADER_REQUEST_ID])
assertThat(request.description).isEqualTo("Logs Request")
assertThat(request.body).isEqualTo(
assertThat(request?.headers?.get(RequestFactory.HEADER_REQUEST_ID)).isNotEmpty()
assertThat(request?.id).isEqualTo(request?.headers?.get(RequestFactory.HEADER_REQUEST_ID))
assertThat(request?.description).isEqualTo("Logs Request")
assertThat(request?.body).isEqualTo(
batchData.map { it.data }
.join(
separator = ",".toByteArray(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ internal class RumRequestFactory(
context: DatadogContext,
batchData: List<RawBatchEvent>,
batchMetadata: ByteArray?
): Request {
): Request? {
val requestId = UUID.randomUUID().toString()

return Request(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,19 +75,19 @@ internal class RumRequestFactoryTest {
val request = testedFactory.create(fakeDatadogContext, batchData, batchMetadata)

// Then
assertThat(request.url).isEqualTo(expectedUrl(fakeDatadogContext.site.intakeEndpoint))
assertThat(request.contentType).isEqualTo(RequestFactory.CONTENT_TYPE_TEXT_UTF8)
assertThat(request.headers.minus(RequestFactory.HEADER_REQUEST_ID)).isEqualTo(
assertThat(request?.url).isEqualTo(expectedUrl(fakeDatadogContext.site.intakeEndpoint))
assertThat(request?.contentType).isEqualTo(RequestFactory.CONTENT_TYPE_TEXT_UTF8)
assertThat(request?.headers?.minus(RequestFactory.HEADER_REQUEST_ID)).isEqualTo(
mapOf(
RequestFactory.HEADER_API_KEY to fakeDatadogContext.clientToken,
RequestFactory.HEADER_EVP_ORIGIN to fakeDatadogContext.source,
RequestFactory.HEADER_EVP_ORIGIN_VERSION to fakeDatadogContext.sdkVersion
)
)
assertThat(request.headers[RequestFactory.HEADER_REQUEST_ID]).isNotEmpty()
assertThat(request.id).isEqualTo(request.headers[RequestFactory.HEADER_REQUEST_ID])
assertThat(request.description).isEqualTo("RUM Request")
assertThat(request.body).isEqualTo(
assertThat(request?.headers?.get(RequestFactory.HEADER_REQUEST_ID)).isNotEmpty()
assertThat(request?.id).isEqualTo(request?.headers?.get(RequestFactory.HEADER_REQUEST_ID))
assertThat(request?.description).isEqualTo("RUM Request")
assertThat(request?.body).isEqualTo(
batchData.map { it.data }.join(
separator = "\n".toByteArray(),
internalLogger = InternalLogger.UNBOUND
Expand Down Expand Up @@ -115,19 +115,19 @@ internal class RumRequestFactoryTest {
val request = testedFactory.create(fakeDatadogContext, batchData, batchMetadata)

// Then
assertThat(request.url).isEqualTo(expectedUrl(fakeEndpoint))
assertThat(request.contentType).isEqualTo(RequestFactory.CONTENT_TYPE_TEXT_UTF8)
assertThat(request.headers.minus(RequestFactory.HEADER_REQUEST_ID)).isEqualTo(
assertThat(request?.url).isEqualTo(expectedUrl(fakeEndpoint))
assertThat(request?.contentType).isEqualTo(RequestFactory.CONTENT_TYPE_TEXT_UTF8)
assertThat(request?.headers?.minus(RequestFactory.HEADER_REQUEST_ID)).isEqualTo(
mapOf(
RequestFactory.HEADER_API_KEY to fakeDatadogContext.clientToken,
RequestFactory.HEADER_EVP_ORIGIN to fakeDatadogContext.source,
RequestFactory.HEADER_EVP_ORIGIN_VERSION to fakeDatadogContext.sdkVersion
)
)
assertThat(request.headers[RequestFactory.HEADER_REQUEST_ID]).isNotEmpty()
assertThat(request.id).isEqualTo(request.headers[RequestFactory.HEADER_REQUEST_ID])
assertThat(request.description).isEqualTo("RUM Request")
assertThat(request.body).isEqualTo(
assertThat(request?.headers?.get(RequestFactory.HEADER_REQUEST_ID)).isNotEmpty()
assertThat(request?.id).isEqualTo(request?.headers?.get(RequestFactory.HEADER_REQUEST_ID))
assertThat(request?.description).isEqualTo("RUM Request")
assertThat(request?.body).isEqualTo(
batchData.map { it.data }.join(
separator = "\n".toByteArray(),
internalLogger = InternalLogger.UNBOUND
Expand Down
Loading

0 comments on commit 67c70a6

Please sign in to comment.