Skip to content

Commit

Permalink
Move helpers in core module that are only used in the interceptor to …
Browse files Browse the repository at this point in the history
…that class
  • Loading branch information
bidetofevil committed Oct 27, 2023
1 parent 4087577 commit f584ad2
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package io.embrace.android.embracesdk.okhttp3
import io.embrace.android.embracesdk.Embrace
import io.embrace.android.embracesdk.InternalApi
import io.embrace.android.embracesdk.internal.network.http.EmbraceHttpPathOverride
import io.embrace.android.embracesdk.internal.utils.causeMessage
import io.embrace.android.embracesdk.internal.utils.causeName
import io.embrace.android.embracesdk.network.EmbraceNetworkRequest
import io.embrace.android.embracesdk.network.http.HttpMethod
import okhttp3.Interceptor
Expand Down Expand Up @@ -92,5 +90,21 @@ public class EmbraceOkHttp3ApplicationInterceptor internal constructor(
const val TRACEPARENT_HEADER_NAME = "traceparent"
const val UNKNOWN_EXCEPTION = "Unknown"
const val UNKNOWN_MESSAGE = "An error occurred during the execution of this network request"

/**
* Return the canonical name of the cause of a [Throwable]. Handles null elements throughout,
* including the throwable and its cause, in which case [defaultName] is returned
*/
fun causeName(throwable: Throwable?, defaultName: String = ""): String {
return throwable?.cause?.javaClass?.canonicalName ?: defaultName
}

/**
* Return the message of the cause of a [Throwable]. Handles null elements throughout,
* including the throwable and its cause, in which case [defaultMessage] is returned
*/
fun causeMessage(throwable: Throwable?, defaultMessage: String = ""): String {
return throwable?.cause?.message ?: defaultMessage
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import io.embrace.android.embracesdk.internal.network.http.NetworkCaptureData
import io.embrace.android.embracesdk.network.EmbraceNetworkRequest
import io.embrace.android.embracesdk.okhttp3.EmbraceOkHttp3ApplicationInterceptor.Companion.UNKNOWN_EXCEPTION
import io.embrace.android.embracesdk.okhttp3.EmbraceOkHttp3ApplicationInterceptor.Companion.UNKNOWN_MESSAGE
import io.embrace.android.embracesdk.okhttp3.EmbraceOkHttp3ApplicationInterceptor.Companion.causeMessage
import io.embrace.android.embracesdk.okhttp3.EmbraceOkHttp3ApplicationInterceptor.Companion.causeName
import io.embrace.android.embracesdk.okhttp3.EmbraceOkHttp3NetworkInterceptor.Companion.CONTENT_ENCODING_HEADER_NAME
import io.embrace.android.embracesdk.okhttp3.EmbraceOkHttp3NetworkInterceptor.Companion.CONTENT_LENGTH_HEADER_NAME
import io.embrace.android.embracesdk.okhttp3.EmbraceOkHttp3NetworkInterceptor.Companion.CONTENT_TYPE_EVENT_STREAM
Expand Down Expand Up @@ -422,6 +424,48 @@ internal class EmbraceOkHttp3InterceptorsTest {
assertEquals(CUSTOM_TRACEPARENT, capturedEmbraceNetworkRequest.captured.w3cTraceparent)
}

@Test
fun `test throwableName`() {
assertEquals("name should be empty string if the Throwable is null", causeName(null), "")
assertEquals(
"name should be empty string if the Throwable's cause is null",
causeName(RuntimeException("message", null)),
""
)
assertEquals(
"name is unexpected",
causeName(
RuntimeException("message", IllegalArgumentException())
),
IllegalArgumentException::class.qualifiedName
)
}

@Test
fun `test throwableMessage`() {
assertEquals(
"message should be empty string if Throwable is null",
causeMessage(null),
""
)
assertEquals(
"message should be empty string if the Throwable's cause is null",
causeMessage(RuntimeException("message", null)),
""
)
assertEquals(
"message should be empty string if the Throwable's cause's message is null",
causeMessage(RuntimeException("message", IllegalArgumentException())),
""
)
val message = "this is a message"
assertEquals(
"message is unexpected",
causeMessage(RuntimeException("message", IllegalArgumentException(message))),
message
)
}

private fun createBaseMockResponse(httpStatus: Int = 200) =
MockResponse()
.setResponseCode(httpStatus)
Expand Down
7 changes: 0 additions & 7 deletions embrace-android-sdk/api/embrace-android-sdk.api
Original file line number Diff line number Diff line change
Expand Up @@ -233,13 +233,6 @@ public final class io/embrace/android/embracesdk/internal/network/http/NetworkCa
public fun toString ()Ljava/lang/String;
}

public final class io/embrace/android/embracesdk/internal/utils/ThrowableUtilsKt {
public static final fun causeMessage (Ljava/lang/Throwable;Ljava/lang/String;)Ljava/lang/String;
public static synthetic fun causeMessage$default (Ljava/lang/Throwable;Ljava/lang/String;ILjava/lang/Object;)Ljava/lang/String;
public static final fun causeName (Ljava/lang/Throwable;Ljava/lang/String;)Ljava/lang/String;
public static synthetic fun causeName$default (Ljava/lang/Throwable;Ljava/lang/String;ILjava/lang/Object;)Ljava/lang/String;
}

public final class io/embrace/android/embracesdk/network/EmbraceNetworkRequest {
public static fun fromCompletedRequest (Ljava/lang/String;Lio/embrace/android/embracesdk/network/http/HttpMethod;JJJJI)Lio/embrace/android/embracesdk/network/EmbraceNetworkRequest;
public static fun fromCompletedRequest (Ljava/lang/String;Lio/embrace/android/embracesdk/network/http/HttpMethod;JJJJILjava/lang/String;)Lio/embrace/android/embracesdk/network/EmbraceNetworkRequest;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,3 @@ internal fun Throwable.getSafeStackTrace(): Array<StackTraceElement>? {
null
}
}

/**
* Return the canonical name of the cause of a [Throwable]. Handles null elements throughout,
* including the throwable and its cause, in which case [defaultName] is returned
*/
@InternalApi
public fun causeName(throwable: Throwable?, defaultName: String = ""): String {
return throwable?.cause?.javaClass?.canonicalName ?: defaultName
}

/**
* Return the message of the cause of a [Throwable]. Handles null elements throughout,
* including the throwable and its cause, in which case [defaultMessage] is returned
*/
@InternalApi
public fun causeMessage(throwable: Throwable?, defaultMessage: String = ""): String {
return throwable?.cause?.message ?: defaultMessage
}
Original file line number Diff line number Diff line change
@@ -1,53 +1,10 @@
package io.embrace.android.embracesdk.internal.utils

import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Test

internal class ThrowableUtilsTest {

@Test
fun `test throwableName`() {
assertEquals("name should be empty string if the Throwable is null", causeName(null), "")
assertEquals(
"name should be empty string if the Throwable's cause is null",
causeName(RuntimeException("message", null)),
""
)
assertEquals(
"name is unexpected",
causeName(
RuntimeException("message", IllegalArgumentException())
),
IllegalArgumentException::class.qualifiedName
)
}

@Test
fun `test throwableMessage`() {
assertEquals(
"message should be empty string if Throwable is null",
causeMessage(null),
""
)
assertEquals(
"message should be empty string if the Throwable's cause is null",
causeMessage(RuntimeException("message", null)),
""
)
assertEquals(
"message should be empty string if the Throwable's cause's message is null",
causeMessage(RuntimeException("message", IllegalArgumentException())),
""
)
val message = "this is a message"
assertEquals(
"message is unexpected",
causeMessage(RuntimeException("message", IllegalArgumentException(message))),
message
)
}

@Test
fun `test safe stacktrace`() {
assertNull(DangerousException().getSafeStackTrace())
Expand Down

0 comments on commit f584ad2

Please sign in to comment.