Skip to content

Commit

Permalink
Catch exceptions when retrieving data from Activity (#669)
Browse files Browse the repository at this point in the history
  • Loading branch information
yangyansong-adbe authored May 23, 2024
1 parent e2563e0 commit e550425
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import org.junit.Assert.assertEquals
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import java.io.Serializable

@RunWith(AndroidJUnit4::class)
class DataMarshallerTests {
Expand Down Expand Up @@ -176,6 +177,23 @@ class DataMarshallerTests {

@Test
fun marshalInvalidUrl_NoCrash() {
val throwsException = ObjectThrowsOnToString()
val intent =
Intent(ApplicationProvider.getApplicationContext(), TestActivity::class.java).apply {
putExtra("key", "value")
putExtra("exceptionKey", throwsException)
}

val activity = activityTestRule.launchActivity(intent)
val result = DataMarshaller.marshal(activity)
assertEquals(
mapOf("key" to "value"),
result
)
}

@Test
fun marshal_whenBundleThrowException_NoCrash() {
val intent =
Intent(ApplicationProvider.getApplicationContext(), TestActivity::class.java).apply {
data = Uri.parse("abc:abc")
Expand All @@ -188,7 +206,11 @@ class DataMarshallerTests {
result
)
}

private class ObjectThrowsOnToString : Serializable {
override fun toString(): String {
throw IllegalStateException("This is a test exception")
}
}
companion object {
const val LEGACY_PUSH_MESSAGE_ID = "adb_m_id"
const val PUSH_MESSAGE_ID_KEY = "pushmessageid"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,13 @@ internal object DataMarshaller {
NOTIFICATION_IDENTIFIER_KEY -> LOCAL_NOTIFICATION_ID_KEY
else -> key
}
val value = extraBundle[key]
if (value?.toString()?.isNotEmpty() == true) {
marshalledData[newKey] = value
try {
val value = extraBundle[key]
if (value?.toString()?.isNotEmpty() == true) {
marshalledData[newKey] = value
}
} catch (e: Exception) {
Log.error(CoreConstants.LOG_TAG, LOG_TAG, "Failed to retrieve data (key = $key) from Activity, error is: ${e.message}")
}
}

Expand Down

0 comments on commit e550425

Please sign in to comment.